java solution using map


  • 0
    P
    class Solution {
    	public boolean isIsomorphic(String s, String t) {
    
    		Map<String, String> map = new HashMap<>();
    		String answer = "";
    		if (s.length() != s.length()) {
    
    			return false;
    		}
    
    		for (int i = 0; i < s.length(); i++) {
    			String temp = "";
    			if (map.containsKey(Character.toString(s.charAt(i)))) {
    				temp = map.get(Character.toString(s.charAt(i)));
    				if (!temp.equals(Character.toString(t.charAt(i)))) {
    					return false;
    				}
    			}else if (map.containsValue(Character.toString(t.charAt(i)))){
    
    				return false;
    			}
    			else {
    				map.put(Character.toString(s.charAt(i)),
    						Character.toString(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.