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.
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: true
思路:题目的意思是判断是否能够通过变化string s中的字符得到string t。
即判断当前需要为i的s[i]和t[i]字符的个数是否相等。
由于全是单个字符,故而使用一个128大小的字符数组来保存出现次数,每一次进行判断。
bool isIsomorphic(string s, string t) {
char s_map[128]={0};
char t_map[128]={0};
int size = s.size();
for(int i=0;i<size;i++){
if (s_map[s[i]]!=t_map[t[i]]) return false;
s_map[s[i]] = i+1;
t_map[t[i]] = i+1;
}
return true;
}