Accepted JAVA Solution With Two ArrayList


  • 0
    F
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
           if(s.length()!=t.length()){return false;}
           List<Character> character = new ArrayList<Character>();
           List<Character> replace = new ArrayList<Character>();
            for(int i=0; i<s.length(); i++){
                char sChar = s.charAt(i);
                char tChar = t.charAt(i);
                if(!character.contains(sChar) && !replace.contains(tChar)){
                    character.add(sChar);
                    replace.add(tChar);
                }
                else if(character.indexOf(sChar) != replace.indexOf(tChar)){
                        return false;
                }
            }
            return true;
        }
    }

  • 0
    K

    I saw this one-liner, ye shi zui le!
    s.find(c) returns index of first char c in string s.

    def isIsomorphic(self, s, t):
        return [s.find(c) for c in s] == [t.find(c) for c in t]
    

Log in to reply
 

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