Logistic-clear Java solution by using ArrayList


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

Log in to reply
 

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