-
Notifications
You must be signed in to change notification settings - Fork 8
/
IsomorphicStrings.java
55 lines (51 loc) · 1.66 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package string;
// Source : https://leetcode.com/problems/isomorphic-strings/
// Id : 205
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2021/2/11
// Topic : string
// Level : Easy-
// Other :
// Tips : Isomorphic 同构。
// Links :
// Result : 81.95% 34.68%
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class IsomorphicStrings {
// 43.61% 17ms 32.16%
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length())
return false;
if (s.length() == 0)
return true;
Map<Character, Character> s2tmap = new HashMap<>(26);
Set<Character> mapedT = new HashSet<>(26);
for (int i = 0; i < s.length(); i++) {
if (s2tmap.containsKey(s.charAt(i))) {
if (!s2tmap.get(s.charAt(i)).equals(t.charAt(i)))
return false;
} else {
if (mapedT.contains(t.charAt(i)))
return false;
s2tmap.put(s.charAt(i), t.charAt(i));
mapedT.add(t.charAt(i));
}
}
return true;
}
// 81.95% 7ms 34.68%
public boolean isIsomorphic1(String s, String t) {
// public boolean isIsomorphic(String s, String t) {
int[] sPlacement = new int[128];
int[] tPlacement = new int[128];
int NOT_ZERO = 1;
for (int i = 0; i < s.length(); i++) {
if (sPlacement[s.charAt(i)] != tPlacement[t.charAt(i)])
return false;
sPlacement[s.charAt(i)] = tPlacement[t.charAt(i)] = i + NOT_ZERO;
}
return true;
}
}