Java solution(beats 84%), 12 lines in total

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

  • 0

    Nice solution

  • 1

    What if the source & target are large strings?

    Say, s is a piece of Genome sequence while t is a transcripted RNA?

    In this case, using a HashSet seems to be a better solution.
    Apparently this type of test case(large string) is not included in the leetcode test base.

    Having said that, in most case, I would agree the solution posted above is a nice one and good for most use cases.

  • 0

    interesting approach but it's O(n^2)

Log in to reply

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