-
Notifications
You must be signed in to change notification settings - Fork 43
/
IsomorphicStrings.java
34 lines (28 loc) · 1001 Bytes
/
IsomorphicStrings.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
package LeetCodeJava.HashTable;
// https://leetcode.com/problems/isomorphic-strings/
import java.util.HashMap;
public class IsomorphicStrings {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) {
return false;
}
// NOTE : we have to do both case
return check(s, t) && check(t, s);
}
private boolean check(String s, String t){
HashMap<String, String> map = new HashMap();
for (int i = 0; i < s.length(); i++){
String sValue = String.valueOf(s.charAt(i));
String tValue = String.valueOf(t.charAt(i));
if (!map.containsKey(sValue)){
map.put(sValue, tValue);
}else{
if (! tValue.equals(map.get(sValue))){
//System.out.println("tValue = " + tValue + " map.get(sValue) = " + map.get(sValue));
return false;
}
}
}
return true;
}
}