Isomorphic Strings
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 "egg"
, "add"
return True.
Given "foo"
, "bar"
return FALSE.
Given "paper"
, "title"
return True.
Note:
Assume both s and T have the same length.
Returns true to meet two conditions:
1. A character in s cannot appear corresponding to two different characters in T
2, can not appear in s two different characters corresponding to the same character in T
classSolution { Public: BOOLIsisomorphic (stringSstringt) {intm =s.size (); intn =t.size (); if(M! =N)return false; Unordered_map<Char,Char> m1;//--- tunordered_map<Char,Char> m2;//T --s for(inti =0; I < m; i + +) { CharSchar =S[i]; CharTCHAR =T[i]; //The same char in S map to different chars in T if(M1.find (schar)! = M1.end () && M1[schar]! =TCHAR)return false; //different chars in S map tp the same char in T if(M2.find (TCHAR)! = M2.end () && M2[tchar]! =Schar)return false; M1[schar]=TCHAR; M2[tchar]=Schar; } return true; }};
"Leetcode" 205. Isomorphic Strings