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;
}
};
本文介绍了一个C++实现的算法,用于判断两个字符串是否为同构字符串。即一个字符串中的字符是否能被替换得到另一个字符串,同时保持字符出现的顺序不变。
1414

被折叠的 条评论
为什么被折叠?



