Java Solution using indexOf() method


  • 0
    D

    Three lines Java Solution. The idea is if two strings are isomorphic, the index of first occurrence of each character in one string(s) always matches with the index in the other string(t). Luckily it works!

    public class Solution {
        public boolean isIsomorphic(String s, String t) {
           for(int i=0; i<s.length();i++)
               if(s.indexOf(s.charAt(i))!=t.indexOf(t.charAt(i))) return false;
           return true;
        }
    }
    

Log in to reply
 

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