-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIsomorphic.java
More file actions
47 lines (45 loc) · 1.49 KB
/
Copy pathCheckIsomorphic.java
File metadata and controls
47 lines (45 loc) · 1.49 KB
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
40
41
42
43
44
45
46
47
import java.util.HashMap;
import java.util.Map;
/**
* Created by akumar6 on 7/19/15.
*/
public class CheckIsomorphic {
public static void main(String[] args) {
System.out.println(isIsoMorphic("foo", "bpp"));
System.out.println(isIsoMorphic("foo", "bpe"));
System.out.println(isIsoMorphic("f", "bpe"));
System.out.println(isIsoMorphic("fo", "bpe"));
System.out.println(isIsoMorphic("fapaf", "fbqbf"));
System.out.println(isIsoMorphic("fapaf", "fbqcg"));
}
private static boolean isIsoMorphic(String firstWord, String secondWord) {
if (firstWord == null || secondWord == null) {
return false;
}
if (firstWord.length() != secondWord.length()) {
return false;
}
if (firstWord.length() == 0 && secondWord.length() == 0) {
return true;
}
Map<Character, Character> m1 = new HashMap();
Map<Character, Character> m2 = new HashMap();
for (int i = 0; i < firstWord.length(); i++) {
Character c1 = firstWord.charAt(i);
Character c2 = secondWord.charAt(i);
if (m1.containsKey(c1)) {
if (m1.get(c1) != c2) {
return false;
}
}
if (m2.containsKey(c2)) {
if (m2.get(c2) != c1) {
return false;
}
}
m1.put(c1,c2);
m2.put(c2,c1);
}
return true;
}
}