https://leetcode.com/problems/isomorphic-strings/
Input: s = "egg", t = "add
Output: true
Input: s = "foo", t = "bar"
Output: false
들렀는지 안 들렀는지 marking이 필요했다.
class Solution {
public:
bool isIsomorphic(string s, string t) {
std::unordered_map<char, char> cmap;
bool isVisited[128] = { 0 };
for (int i = 0; i < s.size(); ++i)
{
if (cmap.find(s[i]) != cmap.end())
{
if (cmap[s[i]] != t[i]) return false;
}
else
{
if (isVisited[t[i]]) return false; // 못찾았는데 이미 방문했다고 뜬다면 다른값이 들어온 것이다.
cmap[s[i]] = t[i];
isVisited[t[i]] = true;
}
}
return true;
}
};
bool 공간을 따로 두었다. 들어가지도 않았던 값인데, 이미 방문했다고 뜨면 false 해준다.
Runtime: 4 ms, faster than 94.89% of C++ online submissions for Isomorphic Strings.
Memory Usage: 7 MB, less than 69.66% of C++ online submissions for Isomorphic Strings
Runtime: 8 ms, faster than 68.99% of C++ online submissions for Isomorphic Strings.
Memory Usage: 6.9 MB, less than 92.53% of C++ online submissions for Isomorphic Strings.