Skip to content

Commit 3e0fd11

Browse files
refactor: refactoring and documenting Isomorphic String Checker (#6359)
refactor: refactoring and documenting Isomorphic String Checker Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
1 parent 2ccc156 commit 3e0fd11

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,54 @@
55
import java.util.Map;
66
import java.util.Set;
77

8+
/**
9+
* Utility class to check if two strings are isomorphic.
10+
*
11+
* <p>
12+
* Two strings {@code s} and {@code t} are isomorphic if the characters in {@code s}
13+
* can be replaced to get {@code t}, while preserving the order of characters.
14+
* Each character must map to exactly one character, and no two characters can map to the same character.
15+
* </p>
16+
*
17+
* @see <a href="https://en.wikipedia.org/wiki/Isomorphism_(computer_science)">Isomorphic Strings</a>
18+
*/
819
public final class Isomorphic {
20+
921
private Isomorphic() {
1022
}
1123

12-
public static boolean checkStrings(String s, String t) {
24+
/**
25+
* Checks if two strings are isomorphic.
26+
*
27+
* @param s the first input string
28+
* @param t the second input string
29+
* @return {@code true} if {@code s} and {@code t} are isomorphic; {@code false} otherwise
30+
*/
31+
public static boolean areIsomorphic(String s, String t) {
1332
if (s.length() != t.length()) {
1433
return false;
1534
}
1635

17-
// To mark the characters of string using MAP
18-
// character of first string as KEY and another as VALUE
19-
// now check occurence by keeping the track with SET data structure
20-
Map<Character, Character> characterMap = new HashMap<>();
21-
Set<Character> trackUniqueCharacter = new HashSet<>();
36+
Map<Character, Character> map = new HashMap<>();
37+
Set<Character> usedCharacters = new HashSet<>();
2238

2339
for (int i = 0; i < s.length(); i++) {
24-
if (characterMap.containsKey(s.charAt(i))) {
25-
if (t.charAt(i) != characterMap.get(s.charAt(i))) {
40+
char sourceChar = s.charAt(i);
41+
char targetChar = t.charAt(i);
42+
43+
if (map.containsKey(sourceChar)) {
44+
if (map.get(sourceChar) != targetChar) {
2645
return false;
2746
}
2847
} else {
29-
if (trackUniqueCharacter.contains(t.charAt(i))) {
48+
if (usedCharacters.contains(targetChar)) {
3049
return false;
3150
}
32-
33-
characterMap.put(s.charAt(i), t.charAt(i));
51+
map.put(sourceChar, targetChar);
52+
usedCharacters.add(targetChar);
3453
}
35-
trackUniqueCharacter.add(t.charAt(i));
3654
}
55+
3756
return true;
3857
}
3958
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public final class IsomorphicTest {
1212
@ParameterizedTest
1313
@MethodSource("inputs")
1414
public void testCheckStrings(String str1, String str2, Boolean expected) {
15-
assertEquals(expected, Isomorphic.checkStrings(str1, str2));
16-
assertEquals(expected, Isomorphic.checkStrings(str2, str1));
15+
assertEquals(expected, Isomorphic.areIsomorphic(str1, str2));
16+
assertEquals(expected, Isomorphic.areIsomorphic(str2, str1));
1717
}
1818

1919
private static Stream<Arguments> inputs() {

0 commit comments

Comments
 (0)