-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution205.java
More file actions
64 lines (56 loc) · 1.73 KB
/
Copy pathSolution205.java
File metadata and controls
64 lines (56 loc) · 1.73 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
64
/**
* @Title: Solution205.java——
* @Package EasyCode
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月25日 下午8:40:01
* @version V1.0
*/
package EasyCode;
import java.util.HashMap;
import java.util.HashSet;
/**
* @ClassName: Solution205——同构字符串
* 给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。
两个字符不能映射到同一个字符上,但字符可以映射自己本身。
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月25日 下午8:40:01
*/
public class Solution205 {
public static boolean isIsomorphic(String s, String t) {
if(s.length() != t.length())
return false;
if(s == null && t == null)
return true;
if(s == null && t != null)
return false;
if(s != null && t == null)
return false;
HashSet<Character> set = new HashSet<>(s.length());
HashMap<Character, Character> map = new HashMap<>(s.length());
for(int i = 0; i < s.length() ; i ++){
//不存在键值,就建立映射
if(!map.containsKey(s.charAt(i))){
//如果要存入的键不存在,但是对应的值却已经存在了,直接返回false
if(set.contains(t.charAt(i))) return false;
else{
map.put(s.charAt(i) , t.charAt(i));
set.add(t.charAt(i));
}
}
else{//存在键值了,看t字符串中的字符是否和键中存储的值相等
if(map.get(s.charAt(i)) != t.charAt(i))
return false;
}
}
return true;
}
public static void main(String[] args) {
String s = "ab";
String t = "aa";
System.out.println(isIsomorphic(s, t));
}
}