Skip to content

Commit 5c1da29

Browse files
renamed function names and few fixes w.r.t to suggestion
Signed-off-by: JeevaRamanathan <jeevaramanathan.m@infosys.com>
1 parent c45d3b3 commit 5c1da29

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,28 @@ private Isogram() {
4343
* @throws IllegalArgumentException if the string contains non-alphabetic
4444
* characters
4545
*/
46-
public static boolean isIsogramByArray(String str) {
46+
public static boolean isAlphabeticIsogram(String str) {
4747
if (str == null || str.isEmpty()) {
4848
return true;
4949
}
5050

51-
String lowerStr = str.toLowerCase();
52-
boolean[] seenChars = new boolean[26];
51+
str = str.toLowerCase();
5352

54-
for (int i = 0; i < lowerStr.length(); i++) {
55-
char ch = lowerStr.charAt(i);
53+
for (int i = 0; i < str.length(); i++) {
54+
char ch = str.charAt(i);
55+
if (ch < 'a' || ch > 'z') {
56+
throw new IllegalArgumentException("Input contains non-alphabetic character: '" + ch + "'");
57+
}
58+
}
5659

57-
// Check if character is a letter
58-
if (ch >= 'a' && ch <= 'z') {
59-
int index = ch - 'a';
60-
if (seenChars[index]) {
61-
return false; // Letter already seen
62-
}
63-
seenChars[index] = true;
60+
boolean[] seenChars = new boolean[26];
61+
for (int i = 0; i < str.length(); i++) {
62+
char ch = str.charAt(i);
63+
int index = ch - 'a';
64+
if (seenChars[index]) {
65+
return false;
6466
}
67+
seenChars[index] = true;
6568
}
6669
return true;
6770
}
@@ -74,7 +77,7 @@ public static boolean isIsogramByArray(String str) {
7477
* @param str the input string
7578
* @return true if the string is an isogram, false otherwise
7679
*/
77-
public static boolean isIsogramByLength(String str) {
80+
public static boolean isFullIsogram(String str) {
7881
if (str == null || str.isEmpty()) {
7982
return true;
8083
}

src/test/java/com/thealgorithms/strings/IsogramTest.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
46
import static org.junit.jupiter.api.Assertions.assertTrue;
57

68
import java.util.stream.Stream;
@@ -13,7 +15,7 @@ public class IsogramTest {
1315
record IsogramTestCase(String input, boolean expected) {
1416
}
1517

16-
private static Stream<IsogramTestCase> isogramArrayTestData() {
18+
private static Stream<IsogramTestCase> isAlphabeticIsogram() {
1719
return Stream.of(
1820
// Valid isograms (only checks letters)
1921
new IsogramTestCase("uncopyrightable", true), new IsogramTestCase("dermatoglyphics", true), new IsogramTestCase("background", true), new IsogramTestCase("python", true), new IsogramTestCase("keyboard", true), new IsogramTestCase("clipboard", true), new IsogramTestCase("flowchart", true),
@@ -30,7 +32,7 @@ private static Stream<IsogramTestCase> isogramArrayTestData() {
3032
new IsogramTestCase("Python", true), new IsogramTestCase("BACKGROUND", true), new IsogramTestCase("Hello", false), new IsogramTestCase("PROGRAMMING", false));
3133
}
3234

33-
private static Stream<IsogramTestCase> isogramLengthTestData() {
35+
private static Stream<IsogramTestCase> isFullIsogram() {
3436
return Stream.of(
3537
// Valid isograms (checks all characters)
3638
new IsogramTestCase("uncopyrightable", true), new IsogramTestCase("dermatoglyphics", true), new IsogramTestCase("background", true), new IsogramTestCase("python", true), new IsogramTestCase("keyboard", true), new IsogramTestCase("clipboard", true), new IsogramTestCase("flowchart", true),
@@ -60,34 +62,56 @@ private static Stream<IsogramTestCase> isogramLengthTestData() {
6062
}
6163

6264
@ParameterizedTest
63-
@MethodSource("isogramArrayTestData")
65+
@MethodSource("isAlphabeticIsogram")
6466
void testIsogramByArray(IsogramTestCase testCase) {
65-
assertEquals(testCase.expected(), Isogram.isIsogramByArray(testCase.input()));
67+
assertEquals(testCase.expected(), Isogram.isAlphabeticIsogram(testCase.input()));
6668
}
6769

6870
@ParameterizedTest
69-
@MethodSource("isogramLengthTestData")
71+
@MethodSource("isFullIsogram")
7072
void testIsogramByLength(IsogramTestCase testCase) {
71-
assertEquals(testCase.expected(), Isogram.isIsogramByLength(testCase.input()));
73+
assertEquals(testCase.expected(), Isogram.isFullIsogram(testCase.input()));
7274
}
7375

7476
@Test
7577
void testNullInputByArray() {
76-
assertTrue(Isogram.isIsogramByArray(null));
78+
assertTrue(Isogram.isAlphabeticIsogram(null));
7779
}
7880

7981
@Test
8082
void testNullInputByLength() {
81-
assertTrue(Isogram.isIsogramByLength(null));
83+
assertTrue(Isogram.isFullIsogram(null));
8284
}
8385

8486
@Test
8587
void testEmptyStringByArray() {
86-
assertTrue(Isogram.isIsogramByArray(""));
88+
assertTrue(Isogram.isAlphabeticIsogram(""));
8789
}
8890

8991
@Test
9092
void testEmptyStringByLength() {
91-
assertTrue(Isogram.isIsogramByLength(""));
93+
assertTrue(Isogram.isFullIsogram(""));
94+
}
95+
96+
@Test
97+
void testAlphabeticIsogramThrowsException() {
98+
// Test that IllegalArgumentException is thrown for non-alphabetic characters
99+
assertThrows(IllegalArgumentException.class, () -> Isogram.isAlphabeticIsogram("1"));
100+
assertThrows(IllegalArgumentException.class, () -> Isogram.isAlphabeticIsogram("@"));
101+
assertThrows(IllegalArgumentException.class, () -> Isogram.isAlphabeticIsogram("python!"));
102+
assertThrows(IllegalArgumentException.class, () -> Isogram.isAlphabeticIsogram("123algorithm"));
103+
assertThrows(IllegalArgumentException.class, () -> Isogram.isAlphabeticIsogram("hello123"));
104+
assertThrows(IllegalArgumentException.class, () -> Isogram.isAlphabeticIsogram("!@@#$%^&*()"));
105+
}
106+
107+
@Test
108+
void testFullIsogramWithMixedCharacters() {
109+
// Test that full isogram method handles all character types without exceptions
110+
assertTrue(Isogram.isFullIsogram("abc123"));
111+
assertFalse(Isogram.isFullIsogram("test@email")); // 'e' repeats
112+
assertFalse(Isogram.isFullIsogram("hello123")); // 'l' repeats
113+
assertTrue(Isogram.isFullIsogram("1234567890"));
114+
assertFalse(Isogram.isFullIsogram("12321")); // '1' and '2' repeat
115+
assertTrue(Isogram.isFullIsogram("!@#$%^&*()"));
92116
}
93117
}

0 commit comments

Comments
 (0)