Java Solution, using array as map


  • 2
    Z
    public boolean isIsomorphic(String s, String t) {
    char[] maps = new char[128];
    char[] mapt = new char[128];
    for (int i = 0; i < s.length(); i++) {
        if ((maps[s.charAt(i)] != 0 && maps[s.charAt(i)] != t.charAt(i)) || (mapt[t.charAt(i)] != 0 && mapt[t.charAt(i)] != s.charAt(i))) {
    	return false;
        }
        maps[s.charAt(i)] = t.charAt(i);
        mapt[t.charAt(i)] = s.charAt(i);
    }
    return true;
    }

  • 2
    S
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            char[] maps = new char[128];
            char[] mapt = new char[128];
            for (int i = 0; i < s.length(); i++) {
                if (maps[s.charAt(i)] != mapt[t.charAt(i)]) {
                return false;
                }
                maps[s.charAt(i)] = mapt[t.charAt(i)]=(char)(i+1);
    
            }
            return true;
            }
    }

  • 0
    Z

    I felt my solution can be simplified somehow, your answer is much concise and make scence.


Log in to reply
 

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