-
Notifications
You must be signed in to change notification settings - Fork 19.8k
/
Copy pathIsomorphic.java
39 lines (33 loc) · 1.16 KB
/
Isomorphic.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.thealgorithms.strings;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public final class Isomorphic {
private Isomorphic() {
}
public static boolean checkStrings(String s, String t) {
if (s.length() != t.length()) {
return false;
}
// To mark the characters of string using MAP
// character of first string as KEY and another as VALUE
// now check occurence by keeping the track with SET data structure
Map<Character, Character> characterMap = new HashMap<>();
Set<Character> trackUniqueCharacter = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (characterMap.containsKey(s.charAt(i))) {
if (t.charAt(i) != characterMap.get(s.charAt(i))) {
return false;
}
} else {
if (trackUniqueCharacter.contains(t.charAt(i))) {
return false;
}
characterMap.put(s.charAt(i), t.charAt(i));
}
trackUniqueCharacter.add(t.charAt(i));
}
return true;
}
}