-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsomorphic Strings
80 lines (60 loc) · 1.74 KB
/
Isomorphic Strings
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.HashMap;
import java.util.HashSet;
//****Leet code 205
public class Solution {
public static void main (String args[]){
Solution s1 = new Solution();
String s = "egg";
String t = "add";
System.out.println(s1.isIsomorphic(s , t));
}
public boolean isIsomorphic(String s, String t) {
if(s.length()!= t.length()){
return false;
}
char[] arr1 = new char[256];
char[] arr2 = new char[256];
for(int i = 0; i < s.length() ; i++){
char ch1 = s.charAt(i);
char ch2 = t.charAt(i);
if(arr1[(int)ch1] == '\u0000' && arr2[(int)ch2] == '\u0000') {
arr1[(int)ch1] = t.charAt(i);
arr2[(int)ch2] = s.charAt(i);
//return true;
}
else{
if(arr1[(int)ch1] == t.charAt(i) && arr2[(int)ch2] == s.charAt(i)){
}
else{
return false;
}
}
}
return true;
}
public boolean isIsomorphic(String s, String t){
HashMap<Character,Character> hm = new HashMap<Character,Character>();
for(int i = 0; i < s.length(); i++){
char a = s.charAt(i);
char b = t.charAt(i);
HashSet<Character> hs = new HashSet<Character>();
if(hm.containsKey(a)){
if(hm.get(a).equals(b)){
continue;
}
else{
return false;
}
}
else{
if(!hm.containsValue(b)){
hm.put(a, b);
}
else{
return false;
}
}
}
return true;
}
}