Accepted Java Solution, Two HashMap


  • 0
    A
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            if(s.length() != t.length()){
                return false;
            }
            char[] s1 = s.toCharArray();
            char[] t1 = t.toCharArray();
            Map<Character, Character> mapS = new HashMap<Character, Character>();
            Map<Character, Character> mapT = new HashMap<Character, Character>();
            for(int i = 0; i < s1.length; i++){
                if(!mapT.containsKey(s.charAt(i))){
                    mapT.put(s.charAt(i), t.charAt(i));
                }
                    s1[i] = mapT.get(s.charAt(i));
                
                if(!mapS.containsKey(t.charAt(i))){
                    mapS.put(t.charAt(i), s.charAt(i));
                }
                t1[i] = mapS.get(t.charAt(i));
            }
            String ss = new String(s1);
            String tt = new String(t1);
            if(s.equals(tt) && t.equals(ss)){
                return true;
            }
            return false;
        }
    }

Log in to reply
 

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