Java Solution with hashmap and easy to understand


  • 0
    P
    public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(transform(s).equals(transform(t)))
            return true;
        else
           return false;
    }
    public String transform(String s){
       StringBuffer sb = new StringBuffer("");
       Map<Character,Character> map = new HashMap<Character,Character>();
       char temp = 'A';
       for(int i = 0;i < s.length();i++){
           if(!map.containsKey(s.charAt(i))){
               map.put(s.charAt(i),temp);
               sb.append(temp);
               temp++;
           }
           else
              sb.append(map.get(s.charAt(i)));
       }
       return sb.toString();
    }
    

    }


Log in to reply
 

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