Simple Java solution, clean code


  • 4
    V
     public boolean isIsomorphic(String s, String t) {
            Map<String, String> map = new HashMap<>();
            String[] sList = s.split("");
            String[] tList = t.split("");
            if (s.length() != t.length()) return false;
            for (int i = 0; i < sList.length; i++) {
                if (!map.containsKey(sList[i])) {
                    map.put(sList[i], tList[i]);
                } else {
                    if (!map.get(sList[i]).equals(tList[i])) return false;
                }
            }
            return map.values().size()== new HashSet<>(map.values()).size();
     }

  • 0
    This post is deleted!

Log in to reply
 

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