题目:
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.
分析:
用数字来代表是否规律一致即可。
代码:
class Solution {
public:
bool isIsomorphic(string s, string t) {
int sAscii[95]={};
int sRes=0,sI=1;
int tAscii[95]={};
int tRes=0,tI=1;
for(int i=0;i<s.length();++i){
if(!sAscii[s[i]-' ']){
sAscii[s[i]-' ']=sI;
sI++;
sRes=sRes*10+sI;
}
else sRes=sRes*10+sAscii[s[i]-' '];
if(!tAscii[t[i]-' ']){
tAscii[t[i]-' ']=tI;
tI++;
tRes=tRes*10+tI;
}
else tRes=tRes*10+tAscii[t[i]-' '];
}
return sRes==tRes;
}
};