image.png

解决思路

Map

  1. class Solution {
  2. public boolean isIsomorphic(String s, String t) {
  3. Map<Character,Character> map = new HashMap<>();
  4. if(s.length()!=t.length())return false;
  5. for(int i = 0;i<s.length();i++){
  6. char ss = s.charAt(i);
  7. char tt = t.charAt(i);
  8. if(map.containsKey(ss)){
  9. if(map.get(ss)!=tt)return false;
  10. }else{
  11. if(map.containsValue(tt))return false;
  12. map.put(ss,tt);
  13. }
  14. }
  15. return true;
  16. }
  17. }