Have no idea why leetcode returns false result.


  • -1
    N

    The problem is so easy and straightforward that I just write a code without any thinking.

    The problem is that leetcode returns a wrong result.

    (I've tested this code in VS, VS returns right result.)

    Any idea?

    bool isIsomorphic(string s, string t) {
    	char map[128] = { 0 };
    
    	for (int i = 0; i<s.length(); i++)
    		if (map[s[i]] == 0) map[s[i]] = t[i];
    		else if (map[s[i]] != t[i]) return false;
    
    	return true;
    }

  • 1
    Z

    I guess you fail in this test case isIsomorphic("ab", "aa"). I also met this problem before. I keep two maps to mapping source and target. see my solution https://leetcode.com/discuss/34054/java-solution-using-array-as-map


  • 0
    P

    Your code does not guarantee the condition "no two characters can map to only one character"


  • 0
    N

    Aha... there is another constraint. Then I can make another map to complete the code. Thx.


  • 0
    N

    Thx. Seems what I need to do is to insert the second array.


  • 0
    N

    Thx Peppe.

    bool isIsomorphic(string s, string t) {
    	char maps[128] = { 0 }, mapt[128] = { 0 };
    
    	for (int i = 0; i<s.length(); i++)
    	{
    		if (maps[s[i]] == 0 && mapt[t[i]] == 0)
    		{
    		    maps[s[i]] = t[i];
    		    mapt[t[i]] = s[i];
    		}
    		else if (maps[s[i]] != t[i] || mapt[t[i]] != s[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.