205. Isomorphic Strings
# Easy
class Solution {
public:
bool isIsomorphic(string s, string t) {
return sT(s, t) && sT(t, s);
}
bool sT(string s, string t) {
unordered_map<char, char> m;
for(int i = 0; i < s.length(); i ++) {
if(m.count(s[i]) == 0) {
m[s[i]] = t[i];
} else {
if(m[s[i]] != t[i]) return false;
}
}
return true;
}
};
在C++中,unordered_map 与 map 的区别:
map 用红黑树存储数据,自动排序,插入删除的操作时会比较省时间,但是对空间的耗费比较大,因为需要记录子节点和父节点。
unordered_map 用哈希表存储,无序,查找比较省时,总体来说很省空间,但插入删除操作会比较费时。
Last updated
Was this helpful?