时间轴

2025-11-13

init


题目:

哈希表代表映射,注意要保存一个映射需要两个哈希表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>
#include <vector>
#include <unordered_map>

using std::string;
using std::unordered_map;

class Solution {
public:
bool isIsomorphic(string s, string t)
{
if (s.size() != t.size()) {
return false;
}
unordered_map<char, char> s2tmap;
unordered_map<char, char> t2smap;
int i, n = s.size();
for (i = 0; i < n; i++) {
if (!s2tmap.count(s[i]) && !t2smap.count(t[i])) {
s2tmap[s[i]] = t[i];
t2smap[t[i]] = s[i];
} else {
if (s2tmap[s[i]] != t[i] || t2smap[t[i]] != s[i]) {
return false;
}
}
}

return true;
}
};