Accepted c++ solution


  • 1
    S
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int s2n[256]={0}, t2n[256]={0};
            for(int i=0; i<s.size(); i++){
                if(s2n[s[i]]>0||t2n[t[i]]>0){
                    if(s2n[s[i]]==t2n[t[i]]) continue;
                    else return false;
                }
                else{
                    s2n[s[i]]=i+1;//map char s[i] to location of s[i] in s, should be the same mapping of t[i] to location t[i] in t
                    t2n[t[i]]=i+1;
                }
                
            }
            return true;
        }
    };

Log in to reply
 

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