leetcode 205. isomorphic strings



  1. Isomorphic Strings:题目链接

方法1:map

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
import java.util.HashMap;
import java.util.Map;

public class Solution1 {
// 时间复杂度O(N)
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map = new HashMap<>();

if (s.length() != t.length()) {
return false;
}

for (int i = 0; i < s.length(); ++i) {
char chs = s.charAt(i);
char cht = t.charAt(i);

if (map.containsKey(chs)) {
if (!map.get(chs).equals(cht)) {
return false;
}
} else {
if (map.containsValue(cht)){
return false;
}
map.put(chs, cht);
}
}
return true;
}

public static void main(String[] args) {
String s = "egg";
String t = "add";
System.out.println(new Solution1().isIsomorphic(s, t));
}
}

pS: 源代码链接