8ms o(n) in C++


  • 4
    G
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
             int size=s.size();
             if (size==0)
                return true;
            char ch[128],ismap[128];
            for(int i=0;i<128;i++)
            {`enter code here`
                ch[i]=-1;
                ismap[i]=-1;
            }
            bool flag=true;
            for(int i=0;i<size;i++)
            {
                if(ch[s[i]]==-1&&ismap[t[i]]==-1)
                {
                    ch[s[i]]=t[i];
                    ismap[t[i]]=s[i];
                
                }else if(ch[s[i]]!=t[i]||ismap[t[i]]!=s[i])
                {
                    flag=false;
                    break;
                }
            }
            return flag;
        }
    };

Log in to reply
 

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