205. Isomorphic Strings
题目
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
Note:
You may assume both s and t have the same length.
算法
采用映射的思想,相同位置的两个字母对应一个数字,一旦之后发现这个两个数字不同了,那就返回false。很牛,我刚开始做的很麻烦。
题解:My 6 lines solution
代码
bool isIsomorphic(char* s, char* t) {
int record1[1000];
int record2[1000];
int i;
for(i=0;i<1000;i++){
record1[i]=0;
record2[i]=0;
}
int s_len = strlen(s);
for(i = 0 ;i<s_len;i++){
if(record1[s[i]]!=record2[t[i]]){
return false;
}
record1[s[i]] = i+1;
record2[t[i]] = i+1;
}
return true;
}