Simple solution which just coulnd't pass 1 test case


  • 0
    A

    Hi,

    The following is my solution:

     public bool IsIsomorphic(string s, string t)
        {
            int[] s1 = new int[s.Length];
            int[] t1 = new int[t.Length];
            bool isI = true;
            if (s1.Length > 0 && t1.Length > 0)
            {
                s1[0] = 0;
                int i = 1;
                int j = 1;
                for (i = 1; i < s.Length; i++)
                {
                    if (s[i] == s[i - 1])
                    {
                        s1[i] = 1;
                    }
                    else
                    {
                        s1[i] = 0;
                    }
                }
                for (j = 1; j < t.Length; j++)
                {
                    if (t[j] == t[j - 1])
                    {
                        t1[j] = 1;
                    }
                    else
                    {
                        t1[j] = 0;
                    }
                }
                for (int k = 0; k < s1.Length; k++)
                {
                    if(s1[k] != t1[k])
                    {
                        isI = false;
                    }
                }
            }
            else
            {
                isI = true;
            }
            return isI;
        }
    

    which passed 29/30 test cases but didn't pass the following test case which is ridiculously long. Shared the code with the input in google drive:
    https://docs.google.com/document/d/1UkG8Rc6VItiihwvqzJdM3uMHIX-BCsslJ_lVklkxvq8/edit?usp=sharing

    Any help would be great.


  • 0
    G

    I think you algorithm is wrong, it does not have anything to do with the ridiculously case. Consider this case:
    "aba"
    "abc"

    Your program would return true, which should be false, since 'a' is already mapped to 'a'.


Log in to reply
 

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