-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphicStrings.java
More file actions
25 lines (23 loc) · 784 Bytes
/
IsomorphicStrings.java
File metadata and controls
25 lines (23 loc) · 784 Bytes
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
package leetcode.string;
import java.util.HashMap;
/**
* @ClassName IsomorphicStrings
* @Description 同构字符串 https://leetcode-cn.com/problems/isomorphic-strings/
* @Author changxuan
* @Date 2020/12/27 下午9:08
**/
public class IsomorphicStrings {
public boolean isIsomorphic(String s, String t) {
// 构造字典 s -> t 的映射
HashMap<Character, Character> dict = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (!dict.containsKey(s.charAt(i))) {
if (dict.containsValue(t.charAt(i))) return false;
dict.put(s.charAt(i), t.charAt(i));
} else {
if (dict.get(s.charAt(i)) != t.charAt(i)) return false;
}
}
return true;
}
}