Isomorphic Strings

时间:2022-06-22
本文章向大家介绍Isomorphic Strings,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. Description

2. Solution

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char, char> m1, m2;
        for(int i = 0; i < s.length(); i++) {
            if(m1.find(s[i]) == m1.end()) {
                m1[s[i]] = t[i];
            }
            else if(m1[s[i]] != t[i]) {
                return false;
            }
            if(m2.find(t[i]) == m2.end()) {
                m2[t[i]] = s[i];
            }
            else if(m2[t[i]] != s[i]) {
                return false;
            }
        }
        return true;
    }
};