0

How can I build a Java method that returns TRUE if all the characters in a String are the same using inbuilt methods - without using Regex, loops, or recursion?

Examples:

aaaaaa --> True
abaaaa --> False
Aaaaaa --> False
Alex Wang
  • 27
  • 2
  • 7
  • take the first character, for example in `abaaa` count ihow many a's are present in `str.length()` if its equal it consist of all same character if not then `return false` – Sumit Sagar Mar 24 '21 at 05:45
  • Why without a loop? You have to somehow iterate all the characters; and recursion would be an awful way to do this. – Andy Turner Mar 24 '21 at 05:52
  • I do not think its possible without a loop. You might find some fancy in-built method, but it would also employ some loop of its own. – AKSingh Mar 24 '21 at 05:54
  • @AndyTurner It's a little challenge for a class, I believe the goal is to learn about built-in methods and method chaining. – Alex Wang Mar 24 '21 at 06:05

6 Answers6

5

You can convert the string to an IntStream using .chars() and check if the stream has distinct count of 1 using .distinct().count() == 1.

String s = "aaaaaa";
boolean isAllCharsSame = s.chars().distinct().count() == 1;

isAllCharsSame will be true if all characters in the string s are same, otherwise false.

Edit:

String s = "aaaaaa";
boolean isAllCharsSame = s.codePoints().distinct().count() == 1;

.chars() won't work for Unicode codepoints like "", use .codePoints() for that. Thanks to @BasilBourque for pointing this out.

Osama A.Rehman
  • 912
  • 1
  • 6
  • 12
1

You can use String.replaceAll()

String s = "aaaaaaa";
boolean same = s.replaceAll("" + s.charAt(0), "").length() == 0;
Sumant Guha
  • 101
  • 2
  • 7
1

tl;dr

if ( "".codePoints().distinct().count() == 1 ) { … }

Code point, not char

Some of the other Answers use char. Unfortunately the char type is obsolete, unable to represent even half of the 143,859 characters in Unicode. Just try using the string "" instead of "aaa".

Instead use code point integer numbers.

Set < Integer > codePointsDistinct = "aaaaaaa".codePoints().boxed().collect( Collectors.toSet());
boolean allSameCharacter = ( codePointsDistinct.size() == 1 ) ;

See that code run live at IdeOne.com.

true

We can make that even more brief by asking the stream to eliminate duplicates by calling .distinct().

boolean allSameCharacter = ( "".codePoints().distinct().count() == 1 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can do it without a loop at all, using recursion. It's awful, but possible:

boolean singleChar(String s) {
  // Add check to make sure there is at least one char.

  char first = s.charAt(0);
  return singleCharRecursive(first, 1, s);
}

boolean singleCharRecursive(char first, int idx, String s) {
  return idx >= s.length()
      || (s.charAt(idx) == first && singleCharRecursive (first, idx+1, s));
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Here's another answer

I believe there maybe another option analyzing off the first character in the string to see if they all match, but this one works.

String t = "aaa";
char c = t.charAt(0);
long cnt = t.chars().filter(ch -> ch == c).count();
System.out.println(cnt==t.length());
  • 1
    You can also do this. `boolean result = t.chars().allMatch(ch -> ch == t.charAt(0));` –  Mar 24 '21 at 07:05
0

You can use replace method:

boolean r = "aaaaaaa".replace("a", "").length() == 0;
System.out.println(r); // true

See also: Easier way to represent indicies in a 2D array