Isomorphic strings c++


  • 0
    P
    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            
            unordered_map<char, char> isomorph;
            unordered_set<char> dest;
            
            for(int i = 0; i < s.size(); i++)
            {
                auto isIn = isomorph.find(s[i]);
                if(isIn != isomorph.end())
                {
                    if(isIn->second != t[i])
                        return false;
                }
                else
                {
                    auto isInDest = dest.find(t[i]);
                    if(isInDest == dest.end())
                    {
                        isomorph[s[i]] = t[i];
                        dest.insert(t[i]);
                    }
                    else
                        return false;
                }
            }
            
            return true;
        }
    };

Log in to reply
 

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