Share an esay understandable solution in java


  • 0
    M
    public class Solution {
        
        /**
         * transform string to format like "abc" such as: egg->abb,paper->abacd
         */
        public String transform(String s) {
            StringBuffer re = new StringBuffer("");
            char tmp = 'a';
            Map<Character,Character> map = new HashMap<Character,Character>();
            for(int i=0;i<s.length();i++) {
                if(!map.containsKey(s.charAt(i))) {
                    map.put(s.charAt(i),tmp);
                    re.append(tmp);
                    tmp++;
                }
                else {
                    re.append(map.get(s.charAt(i)));
                }
            }
            return re.toString();
        }
        
        public boolean isIsomorphic(String s, String t) {
            if(transform(s).equals(transform(t))) return true;
            else return false;
        }
    }

  • 0

    You can compare both transformation in one-pass together so you don't have to really transform it to a final string.


Log in to reply
 

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