My c solution, 0ms ,use two 128 array.


  • 0
    A
    bool isIsomorphic(char* s, char* t)
    {
    	int len = strlen(s);
    	char ss[128] = { 0 }, tt[128] = {0};
    	for (int i = 0; i < len; i++)
    	{
    		char a = s[i];
    		char b = t[i];
    		if (ss[a] == 0 && tt[b] == 0)
    		{
    			ss[a] =b;
    			tt[b] = a;
    			continue;
    		}
    		if (ss[a] == 0 && tt[b] != 0)
    			return 0;
    		if (ss[a] != 0 && tt[b] == 0)
    			return 0;
    		if (ss[a] != b || tt[b] != a)
    			return 0;
    	}
    	return 1;
    }

  • 0

    Actually you can just remove these two blocks to make it more terse:

     if (ss[a] == 0 && tt[b] != 0)
            return 0;
     if (ss[a] != 0 && tt[b] == 0)
            return 0;
    

    bool isIsomorphic(char* s, char* t)
    {
        char ss[128] = { 0 }, tt[128] = {0};
        for (int i = 0; s[i]; i++)
        {
            char a = s[i];
            char b = t[i];
            if (ss[a] == 0 && tt[b] == 0)
            {
                ss[a] =b;
                tt[b] = a;
                continue;
            }
            if (ss[a] != b || tt[b] != a)
                return 0;
        }
        return 1;
    }

Log in to reply
 

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