6. Isomorphic Strings
Example 1:
Input: s = "egg", t = "add"
Output: true
Example 2:
Input: s = "foo", t = "bar"
Output: false
Example 3:
Input: s = "paper", t = "title"
Output: trueSolution: (hashmaps)
class Solution
{
public:
bool isIsomorphic(string s, string t)
{
unordered_map<char, char> m;
for (int i = 0; i < s.length(); i++)
{
if (m.find(s[i]) == m.end())
{
for (auto itr = m.begin(); itr != m.end(); itr++)
{
if (itr->second == t[i])
{
return false;
}
}
m.insert({s[i], t[i]});
}
else
{
char ch = m[s[i]];
if (ch != t[i])
{
return false;
}
}
}
return true;
}
};Efficient Approach: (using hashset and hashmap)
Last updated