"Leetcode" 205-isomorphic Strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.