-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisomorphicString.java
More file actions
63 lines (47 loc) · 1.15 KB
/
Copy pathisomorphicString.java
File metadata and controls
63 lines (47 loc) · 1.15 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package LeetCode;
import java.util.HashMap;
public class isomorphicString {
/**
* @param s
* @param t
* @return
*/
public static boolean isIsomorphic(String s, String t) {
HashMap<Character,Character> hMap = new HashMap<Character,Character>();
int lenS = s.length(), lenT = t.length();
boolean res,res1 ;
if(lenS != lenT)
return false;
res = true;
for(int i=0;i<lenS;i++){
if(!hMap.containsKey(s.charAt(i))){
hMap.put(s.charAt(i), t.charAt(i));
}
else if(hMap.containsKey(s.charAt(i))){
if(hMap.get(s.charAt(i)) != t.charAt(i))
res = false;
}
}
hMap.clear();
res1 = true;
for(int i=0;i<lenT;i++){
if(!hMap.containsKey(t.charAt(i))){
hMap.put(t.charAt(i), s.charAt(i));
}
else if(hMap.containsKey(t.charAt(i))){
if(hMap.get(t.charAt(i)) != s.charAt(i))
res1 = false;
}
}
if(res && res1)
return true;
else
return false;
}
public static void main(String[] args) {
String s = "ab";
String t = "aa";
boolean res = isIsomorphic(s,t);
System.out.println(res);
}
}