I think this can be AC... but TLE in one long test case.


  • 0
    R
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
             
             //I think this will solved but TLE
             if(s.length() != t.length()) return false;
            
             vector<char> hash_s;
             vector<char> hash_t;
             hash_s.push_back(s[0]);
             hash_t.push_back(t[0]);
             int idx = 0;
             
             for(int i = 1; i < s.length(); i++)
             {
                for(; idx < hash_s.size(); idx++)
                {
                    if((hash_s[idx] == s[i] && hash_t[idx] != t[i]) || (hash_t[idx] == t[i] && hash_s[idx] != s[i])) 
                        return false;
                }
                hash_s.push_back(s[i]);
                hash_t.push_back(t[i]);
                idx = 0;
             }
             
             return true;
        }
    };
    

    I have tried some simple case...I think the answer is correct but TLE in one case.
    Can some one fix it?
    The idea is push first element into array, then check the hash table (vector). if not pass the return false.

    I guess the drawback is, when a input string is large. it needs N^2 time to search.


Log in to reply
 

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