Java solution with two HashMap


  • 0
    G
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
        	Map<String,String> map = new HashMap<String,String>();
        	Map<String,String> temp = new HashMap<String,String>();
        	String[] s1 = s.split("");
        	String[] t1 = t.split("");
        	for(int i=0;i<s1.length;i++){
        		if(!(map.containsKey(s1[i])||map.containsValue(t1[i]))){
        			map.put(s1[i], t1[i]);
        			temp.put(t1[i], s1[i]);
        		}else if(map.containsKey(s1[i])){
        			if(!map.get(s1[i]).equals(t1[i]))return false;
        		}else if(map.containsValue(t1[i])){
        			if(!temp.get(t1[i]).equals(s1[i]))return false;
        		}
        	}
        	return true;
        }
    }

Log in to reply
 

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