Problem:
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.
Summary:
Determines whether the given two strings are in the same format.
Solution:
Solution with Leetcode 290 Word Pattern
1 classSolution {2 Public:3 BOOLIsisomorphic (stringSstringt) {4 intLen =s.size ();5unordered_map<Char,Char>m;6 for(inti =0; i < Len; i++) {7 if(M.find (s[i]) = =M.end ()) {8M[s[i]] =T[i];9 }Ten Else if(M[s[i]]! =T[i]) { One return false; A } - } - the for(unordered_map<Char,Char>::iterator i = M.begin (); I! = M.end (); i++) { - for(unordered_map<Char,Char>::iterator j = M.begin (); J! = M.end (); J + +) { - if(I->first! = J->first && I->second = = j->second) { - return false; + } - } + } A return true; at } -};
Leetcode 205 Isomorphic Strings