题目

图片.png

题解

图片.png

  1. class Solution {
  2. public:
  3. bool isIsomorphic(string s, string t) {
  4. unordered_map<char, char> s2t;
  5. unordered_map<char, char> t2s;
  6. int len = s.length();
  7. for (int i = 0; i < len; ++i) {
  8. char x = s[i], y = t[i];
  9. //有一个不满足就false , 容器中存在并且键值对不匹配就是错
  10. if ((s2t.count(x) && s2t[x] != y) || (t2s.count(y) && t2s[y] != x)) {
  11. return false;
  12. }
  13. s2t[x] = y;
  14. t2s[y] = x;
  15. }
  16. return true;
  17. }
  18. };