Cover image for Classic Interview 150 Questions P205 Isomorphic Strings

Classic Interview 150 Questions P205 Isomorphic Strings


Timeline

Timeline

2025-11-13

init

Hash table

Problem:

Hash tables represent mappings. Note that to maintain a mapping, you need two hash tables.

12345678910111213141516171819202122232425262728293031
#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;				int i, n = s.size();		unordered_map<char, char> s2tmap;		unordered_map<char, char> t2smap;		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;	}};
Loading comments…