My Solution in Java


  • 0
    S
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
          if(s.length() != t.length()){
    			return false;
    		}
    		Map<Character, Character> charMap = new HashMap<>();
            for(int i = 0;i < s.length(); i++){
            	if (charMap.containsKey(s.charAt(i))){
            		if ((char)charMap.get(s.charAt(i)) != t.charAt(i)){
            			return false;
            		}
            	}else{
            		if(charMap.containsValue(t.charAt(i))){
            			return false;
            		}
            	}
            	charMap.put(s.charAt(i), t.charAt(i));
            	
            }
    		return true;  
        }
    }

Log in to reply
 

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