Java Isomorphic Strings solution using HashMap


  • 0
    V

    public class Solution {

    public boolean isIsomorphic(String s, String t) {
    
        if(s == null && t == null){
            return true;
        }
        if(s.length() == 0 && t.length() == 0){
            return true;
        }
        if(s.length() != t.length()){
            return false;
        }
        
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        
        for(int i = 0; i < s.length(); i++){
            if(map.containsKey(s.charAt(i))){
                if(t.charAt(i) != map.get(s.charAt(i))){
                    return false;
                }
            }else if(map.containsValue(t.charAt(i))){
                    return false;
            }
            
            map.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.