Skip to content

Commit 5f6510f

Browse files
authored
refactor: CharactersSame (#5396)
1 parent be6b0d8 commit 5f6510f

File tree

2 files changed

+17
-36
lines changed

2 files changed

+17
-36
lines changed

src/main/java/com/thealgorithms/strings/CharactersSame.java

+9-15
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,19 @@ private CharactersSame() {
55
}
66

77
/**
8-
* Driver Code
9-
*/
10-
public static void main(String[] args) {
11-
assert isAllCharactersSame("");
12-
assert !isAllCharactersSame("aab");
13-
assert isAllCharactersSame("aaa");
14-
assert isAllCharactersSame("11111");
15-
}
16-
17-
/**
18-
* check if all the characters of a string are same
8+
* Checks if all characters in the string are the same.
199
*
2010
* @param s the string to check
21-
* @return {@code true} if all characters of a string are same, otherwise
22-
* {@code false}
11+
* @return {@code true} if all characters in the string are the same or if the string is empty, otherwise {@code false}
2312
*/
2413
public static boolean isAllCharactersSame(String s) {
25-
for (int i = 1, length = s.length(); i < length; ++i) {
26-
if (s.charAt(i) != s.charAt(0)) {
14+
if (s.isEmpty()) {
15+
return true; // Empty strings can be considered as having "all the same characters"
16+
}
17+
18+
char firstChar = s.charAt(0);
19+
for (int i = 1; i < s.length(); i++) {
20+
if (s.charAt(i) != firstChar) {
2721
return false;
2822
}
2923
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
54

6-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
77

8-
public class CharacterSameTest {
8+
class CharactersSameTest {
99

10-
@Test
11-
public void isAllCharactersSame() {
12-
String input1 = "aaa";
13-
String input2 = "abc";
14-
String input3 = "1 1 1 1";
15-
String input4 = "111";
16-
String input5 = "";
17-
String input6 = " ";
18-
String input7 = ". ";
19-
20-
assertTrue(CharactersSame.isAllCharactersSame(input1));
21-
assertFalse(CharactersSame.isAllCharactersSame(input2));
22-
assertFalse(CharactersSame.isAllCharactersSame(input3));
23-
assertTrue(CharactersSame.isAllCharactersSame(input4));
24-
assertTrue(CharactersSame.isAllCharactersSame(input5));
25-
assertTrue(CharactersSame.isAllCharactersSame(input6));
26-
assertFalse(CharactersSame.isAllCharactersSame(input7));
10+
@ParameterizedTest
11+
@CsvSource({"aaa, true", "abc, false", "'1 1 1 1', false", "111, true", "'', true", "' ', true", "'. ', false", "'a', true", "' ', true", "'ab', false", "'11111', true", "'ababab', false", "' ', true", "'+++', true"})
12+
void testIsAllCharactersSame(String input, boolean expected) {
13+
assertEquals(CharactersSame.isAllCharactersSame(input), expected);
2714
}
2815
}

0 commit comments

Comments
 (0)