LeetCode 205. Isomorphic Strings

用一个哈希表,记录字母到字母间的映射即可。代码:

class Solution
{
public:
    bool isIsomorphic(string s, string t) 
    {
        for (size_t i = 0; i < s.size(); ++ i)
        {
            if (dict.find(s[i]) == dict.end())
            {
                if (validate_mapping(s[i], t[i]))
                {
                    dict[s[i]] = t[i];
                } else
                {
                    return false;
                }
            } else if (dict[s[i]] != t[i])
            {
                return false;
            }
        }
        return true;
    }
private:
    bool validate_mapping(char from, char to)
    {
        for (auto it = dict.begin(); it != dict.end(); ++ it)
        {
            if (it -> second == to)
            {
                return false;
            }
        }
        return true;
    }
    map<char, char> dict;    
};

你可能感兴趣的:(LeetCode,String)