### 【题目描述】

https://leetcode.cn/problems/isomorphic-strings/

### 【代码】leecode

``````package com.company;
import java.util.*;

// 2023-06-03
class Solution {
public boolean isIsomorphic(String s, String t) {
int n = s.length();
HashMap<Character, Character> map = new HashMap<>();
HashMap<Character, Character> map2 = new HashMap<>();

int len = s.length();
for (int i = 0; i < len;  i++){
char c1 = s.charAt(i);
char c2 = t.charAt(i);
if ((map.containsKey(c1) && map.get(c1) != c2) && (map2.containsKey(c2) && map2.get(c2) != c1)){
return false;
}
map.put(c1, c2);
map2.put(c2, c1);
}
return true;
}
}

public class Test {
public static void main(String[] args) {
new Solution().isIsomorphic("egg", "add"); // 输出: true
new Solution().isIsomorphic("foo", "bar"); // 输出: false
new Solution().isIsomorphic("badc", "baba"); // 输出: false
}
}``````

### 【代码】最简单

``````class Solution {
public boolean isIsomorphic(String s, String t) {
for(int i = 0; i < s.length(); i++){
if(s.indexOf(s.charAt(i)) != t.indexOf(t.charAt(i))){
return false;
}
}
return true;
}
}``````

### 【代码】

``````package com.company;
import java.util.*;

// 2023-06-03
class Solution {
public boolean isIsomorphicHelper(String s, String t) {
int n = s.length();
HashMap<Character, Character> map = new HashMap<>();
for (int i = 0; i < n; i++) {
char c1 = s.charAt(i);
char c2 = s.charAt(i);
if (map.containsKey(c1)) {
if (map.get(c1) != c2) {
return false;
}
} else {
map.put(c1, c2);
}
}
return true;
}
}

public class Test {
public static void main(String[] args) {
//        new Solution().isIsomorphic("egg", "add"); // 输出: true
//        new Solution().isIsomorphic("foo", "bar"); // 输出: false
new Solution().isIsomorphic("badc", "baba"); // 输出: false
}
}``````