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.
Didn't do it.
Without a very clear understanding of the meaning of the topic, the topic is that two strings are isomorphic. I think it's the same string in the same position in T as long as the strings in the string s are the same.
In fact the constraint is more, it is a mapping, like egg,e and a mapping, G and D mapping, then the string s in the case of E, the string t must be a.
So, building a map, using the method you've seen last time, is OK. if (Map.find (S[i]) ==map.end ()) ****else****
However, there are two checks to check if the s->t mapping T is satisfied, and the t->s mapping S is satisfied.
1 classSolution {2 Public:3 BOOLIsisomorphic (stringSstringt) {4map<Char,Char>model;5 intSlength=s.length (), tlength=t.length ();6 if(slength!=tlength)return false;7 for(intI=0; i<slength;i++){8 if(Model.find (S[i]) ==model.end ()) model[s[i]]=T[i];9 Else if(Model[s[i]]!=t[i])return false;Ten } One model.clear (); A for(intI=0; i<slength;i++){ - if(Model.find (T[i]) ==model.end ()) model[t[i]]=S[i]; - Else if(Model[t[i]]!=s[i])return false; the } - return true; - - + } -};
Leetcode isomorphic Strings