This is the same with word pattern question. Java solution.


  • 1
    Z

    In fact, for all the attempts to verify whether it is a one-to-one mapping, we could use the same solution.

    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) return false;
            Map<Character, Integer> map1 = new HashMap<>();
            Map<Character, Integer> map2 = new HashMap<>();
            for (Integer i = 0; i < s.length(); i++)
            {
                if (map1.put(s.charAt(i), i) != map2.put(t.charAt(i), i)) return false;
            }
            return true;
        }
    }
    

  • 0
    Z

    @zzitaI Why is it wrong when i is int instead of Integer?


  • 0
    S

    @zhouhangqi94 Integer may be null , when cmp int and null ,it cause nullpointexception.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.