forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharactersSame.java
More file actions
26 lines (23 loc) · 757 Bytes
/
CharactersSame.java
File metadata and controls
26 lines (23 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.thealgorithms.strings;
public final class CharactersSame {
private CharactersSame() {
}
/**
* Checks if all characters in the string are the same.
*
* @param s the string to check
* @return {@code true} if all characters in the string are the same or if the string is empty, otherwise {@code false}
*/
public static boolean isAllCharactersSame(String s) {
if (s.isEmpty()) {
return true; // Empty strings can be considered as having "all the same characters"
}
char firstChar = s.charAt(0);
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) != firstChar) {
return false;
}
}
return true;
}
}