JavaAlgorithms/Strings/CharactersSame.java

28 lines
684 B
Java
Raw Normal View History

package Strings;
public class CharactersSame {
2020-10-24 18:23:28 +08:00
/** Driver Code */
public static void main(String[] args) {
assert isAllCharactersSame("");
assert !isAllCharactersSame("aab");
assert isAllCharactersSame("aaa");
assert isAllCharactersSame("11111");
}
2020-10-24 18:23:28 +08:00
/**
* check if all the characters of a string are same
*
* @param s the string to check
* @return {@code true} if all characters of a string are same, otherwise {@code false}
*/
public static boolean isAllCharactersSame(String s) {
for (int i = 1, length = s.length(); i < length; ++i) {
if (s.charAt(i) != s.charAt(0)) {
return false;
}
}
2020-10-24 18:23:28 +08:00
return true;
}
}