Simple Java Solution with One HashMap


  • 0
    E
        public class Solution {
        public boolean isIsomorphic(String s, String t) {
          HashMap<Character,Character> map = new HashMap<>();
          char[] sc = s.toCharArray();
          char[] tc = t.toCharArray();
          if(sc.length!=tc.length)
          return false;
          for(int i=0;i<sc.length;i++){
              if(map.containsKey(sc[i])){
                  if(map.get(sc[i])!=tc[i])
                  return false;
              }else{
                  if(map.containsValue(tc[i]))
                  return false;
                  map.put(sc[i],tc[i]);
              }
          }
          return true;
        }
    }

Log in to reply
 

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