Total Accepted: 56840
Total Submissions: 190761
Difficulty: Easy
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg"
, "add"
, return true.
Given "foo"
, "bar"
, return false.
Given "paper"
, "title"
, return true.
Note:
You may assume both s and t have the same length.
Subscribe to see which companies asked this question
Show Similar Problems
Have you met this question in a real interview?
Yes
No
class Solution {
public:
int f[400],ff[400];
bool isIsomorphic(string s, string t) {
memset(f,0,sizeof(f));
memset(ff,0,sizeof(ff));
int len=s.length();
for(int i=0;i<len;i++)
{
if(f[s[i]]){ if(f[s[i]]!=t[i]) return false;}
if(ff[t[i]]){ if(ff[t[i]]!=s[i]) return false;}
f[s[i]]=t[i];
ff[t[i]]=s[i];
}
return true;
}
};