Simple and clear java solution with HashMap~~beat 50%


  • 0
    D

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