Concise JAVA O(n) solution


  • 0
    S
    public class Solution {
    public boolean f(String s, String t){
        HashMap<Character, Character> map = new HashMap();
        char[] ss = s.toCharArray();
        char[] tt = t.toCharArray();
        int len = ss.length;
        if(ss.length == 0) return true;
        for(int i = 0; i < len; i++){
            if(map.containsKey(ss[i]) == false ){
                map.put(ss[i], tt[i] );
            }
            else{
                if(map.get(ss[i] ) != tt[i] ) return false;
            }
        }
        return true;
    }
    
    public boolean isIsomorphic(String s, String t) {
        return f(s,t) && f(t,s);
    }
    

    }


Log in to reply
 

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