C++ 6 ms solution using char arrays


  • 0
    A
    bool isIsomorphic(string s, string t) 
        {
            int s_arr[256]={0}, t_arr[256]={0};
            for(int i=0; i<s.size(); i++)
            {
                if(s_arr[s[i]] == 0 && t_arr[t[i]] == 0)
                {
                    s_arr[s[i]] = t[i];
                    t_arr[t[i]] = s[i];
                }
                else if (s_arr[s[i]] == 0 || t_arr[t[i]] == 0)
                {
                    return false;
                }
                else
                {
                    if(s_arr[s[i]] != t[i] || t_arr[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.