mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
205.同构字符串 添加Java版本
This commit is contained in:
@ -68,6 +68,25 @@ public:
|
|||||||
## Java
|
## Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
class Solution {
|
||||||
|
public boolean isIsomorphic(String s, String t) {
|
||||||
|
Map<Character, Character> map1 = new HashMap<>();
|
||||||
|
Map<Character, Character> map2 = new HashMap<>();
|
||||||
|
for (int i = 0, j = 0; i < s.length(); i++, j++) {
|
||||||
|
if (!map1.containsKey(s.charAt(i))) {
|
||||||
|
map1.put(s.charAt(i), t.charAt(j)); // map1保存 s[i] 到 t[j]的映射
|
||||||
|
}
|
||||||
|
if (!map2.containsKey(t.charAt(j))) {
|
||||||
|
map2.put(t.charAt(j), s.charAt(i)); // map1保存 t[j] 到 s[i]的映射
|
||||||
|
}
|
||||||
|
// 无法映射,返回 false
|
||||||
|
if (map1.get(s.charAt(i)) != t.charAt(j) || map2.get(t.charAt(j)) != s.charAt(i)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
Reference in New Issue
Block a user