0

I'm having problem figuring out how to check from users input letters that were repeated. Program needs to output repeated letter as true and if there isn't any then as false. Program should not count numbers or symbols as repeated.

For example:

  • User input: Chocolate
    Program Output: True

  • User input: 112 cream
    Program Output: False

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
Shon LV
  • 11
  • 1
  • 3

7 Answers7

5

Here is another version, based on answer from @rell but with no HashSet or char[] creation.

private static boolean check(String input) {
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (Character.isLetter(ch) && input.indexOf(ch, i + 1) != -1) {
            return true;
        }
    }
    return false;
}

For smaller input strings, this will most likely be faster because of that. But for longer input strings, the version from @rell is potentially faster as he is using a HashSet with O(1) lookup/insert, and since the loop is O(n) the total is O(n). And my solution is O(n^2) (loop O(n) multiplied with indexOfwith O(n)), worst case input would be something like this abcdefghijklmnopqrstuvwxyzz.

Update Another version with streams.

private static boolean check(String input) {
    IntStream characters = input.codePoints().filter(Character::isLetter);
    return characters
            .distinct()
            .count() == characters.count();
}

Update Fixed bug in stream version

gustf
  • 1,959
  • 13
  • 20
2
private static boolean check(String input) {
    Set<Character> tmp = new HashSet<Character>();
    for(char ch : input.toCharArray()) {
        if (Character.isLetter(ch) && !tmp.add(ch)) {
            return true;
        }
    }
    return false;
}
rell
  • 151
  • 5
0

Try this :

    String username ;
    char[] x = username.toCharArray();
    boolean duplicates=false;
    for (j=0;j<x.length;j++)
      for (k=j+1;k<x.length;k++)
        if (x[k] == x[j])
          duplicates=true
gustf
  • 1,959
  • 13
  • 20
Kumar
  • 182
  • 13
0

We can reduce to Single loop with this.

    boolean checkDuplicates(char[] x)
    {
     Set<char> xSet = new HashSet<char>();
       for (char c : x)
       {
        if (xSet.contains(c)) return true;
        xSet.add(i);
       }
     return false;
    }
Kumar
  • 182
  • 13
0

1.) Sort the character array.

2.) Iterate through the array to see if ith value == (i+1)th value. If any found, return false. Else, return true.

Time complexity: O(nlogn) (for sorting)

Space complexity: O(1)

attaboy182
  • 2,039
  • 3
  • 22
  • 28
0

In order to check that any letter is not repeated more than limited number of times you can use the following method:

public static boolean repeatedMoreThanLimitedLength(String str, int limitLength) {
    Pattern pattern = Pattern.compile("(\\w)\\1+");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        if (matcher.group().length() >= limitLength)
            return true;
    }
    return false;
}
0

Since the stream example from the accepted answer doesn't work in Java 17 (streams can't be reused), let me provide a sample with a stream supplier that worked for me.

Also, I use chars() method instead of .codePoints(), it's more intuitive as for me.

So, let's check, if a string is not an isogram.

import java.util.stream.IntStream;
import java.util.function.Supplier;

public class Isogram {
    private static boolean isNotIsogram(String input) {
        Supplier<IntStream> streamSupplier =
            () -> input.chars().filter(Character::isLetter);
        return streamSupplier.get()
                             .distinct()
                             .count() != streamSupplier.get().count();
    }
bebyx
  • 150
  • 1
  • 6