Given strings s and t, determine if they are isomorphic.
The strings is isomorphic if the characters in s can is replaced to get t.
All occurrences of a character must is replaced with another character while preserving the order of characters. No, characters may map to the same character and a character may map to itself.
For example,
Given "foo", "app"; Returns true we can map F-A and o->p
Given "bar", "foo"; Returns false we can ' t map both ' a ' and ' r ' to ' O '
Given "AB", "Ca"; Returns true we can map ' a ', ' B ' and ' C '
Solution 1:map
classSolution { Public: BOOLIsisomorphic (stringSstringt) {if(S.size ()!=t.size ())return false; Map<Char,Char>m; for(intI=0; I<s.size (); i++){ if(!M.count (S[i])) {Map<Char,Char>::const_iterator iter=M.begin (); while(iter!=M.end ()) { if(Iter->second==t[i])return false; ITER++; } m.insert (Make_pair (S[i], t[i])); }Else{ if(M[s[i]]!=t[i])return false; } } return true; }};
Solution 2:
BOOL Isisomorphic (string s, String t) {
if (S.size ()!=t.size ()) return false;
Map<char,char> Map1;
Map<char,char> map2;
for (int i=0;i<s.length (); i++) {
Char C1=s[i];
Char C2=t[i];
if (Map1.count (C1)) {
if (MAP1[C1]!=C2) return false;
}
if (Map2.count (C2)) {
if (MAP2[C2]!=C1) return false;
}
Map1.insert (c1, C2);
Map2.insert (C2, C1);
}
return true;
}
"Leetcode" 205-isomorphic Strings