Question

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

Thinking:

  • Method:
    • 通过HashMap存储已经出现的字符的第一个位置,创建一个数组用于储存字符串中的每个字符对应的首次出现的位置。
    • 比较数组的值是否完全一致。
class Solution {
    public boolean isIsomorphic(String s, String t) {
        int sLen = s.length();
        int tLen = t.length();
        Map<Character, Integer> exist1 = new HashMap<>();
        char[] arr = s.toCharArray();
        int[] temp = new int[sLen];
        for(int i = 0; i < sLen; i++){
            char c = arr[i];
            if(exist1.containsKey(c))
                temp[i] = exist1.get(c);
            else{
                exist1.put(c, i);
                temp[i] = i;
            }
        }
        exist1.clear();
        arr = t.toCharArray();
        int[] temp1 = new int[tLen];
        for(int i = 0; i < tLen; i++){
            char c = arr[i];
            if(exist1.containsKey(c))
                temp1[i] = exist1.get(c);
            else{
                exist1.put(c, i);
                temp1[i] = i;
            }
        }
        for(int i = 0; i < tLen; i++)
            if(temp[i] != temp1[i]) return false;
        return true;
    }
}
  • Method 2:
    • 考虑到使用的是ASC II,我们使用数组替代哈希表。
    • 同时我们不使用首次出现的位置,而是使用上次出现的位置。
class Solution {
    public boolean isIsomorphic(String s, String t) {
        int[] sarr = new int[256];
        int[] tarr = new int[256];
        for(int i = 0; i < 256; i++){
            sarr[i] = -1;
            tarr[i] = -1;
        }
        int len = s.length();
        for(int i = 0; i < len; i++){
            if(sarr[s.charAt(i)] != tarr[t.charAt(i)]) return false;
            sarr[s.charAt(i)] = tarr[t.charAt(i)] = i;
        }
        return true;
    }
}

二刷

  1. 创建一个转换函数,我们将字符串进行转化,最后比较转化后的字符。
    class Solution {
     public boolean isIsomorphic(String s, String t) {
         return transfer(s).equals(transfer(t));
     }
        
     private String transfer(String s){
         Map<Character, Character> map = new HashMap<>();
         char count = 'a';
         char[] arr = s.toCharArray();
         StringBuilder sb = new StringBuilder();
         for(int i = 0; i < arr.length; i++){
             if(map.containsKey(arr[i])) sb.append(map.get(arr[i]));
             else{
                 map.put(arr[i], count++);
                 sb.append(count - 1);
             }
         }
         return sb.toString();
     }
    }
    
  2. DP: 通过两个数组记录当前字符上一次出现的位置,在比那里两个字符串的同时比较上一次出现的位置。
    class Solution {
     public boolean isIsomorphic(String s, String t) {
         int[] sArr = new int[256];
         int[] tArr = new int[256];
         for(int i = 0; i < 256; i++){
             sArr[i] = -1;
             tArr[i] = -1;
         }
         int len = s.length();
         for(int i = 0; i < len; i++){
             if(sArr[s.charAt(i)] != tArr[t.charAt(i)]) return false;
             sArr[s.charAt(i)] = tArr[t.charAt(i)] = i;
         }
         return true;
     }
    }