Isomorphic Strings Solutions

Source: Internet
Author: User
Question

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.

Solution 1

Use HashMap, remember to check both key and value. Time complexity O (n^2), Space cost O (n).

Hashmap.containsvalue costs O (n)

1  Public classSolution {2      Public Booleanisisomorphic (string s, String t) {3         if(s = =NULL|| t = =NULL)4             return false;5         if(S.length ()! =t.length ())6             return false;7         if(S.length () ==0 && t.length () ==0)8             return true;9         intLength =s.length ();TenMap<character, character> map =NewHashmap<character, character>(); One          for(inti = 0; i < length; i++) { A             CharTMP1 =S.charat (i); -             CharTMP2 =T.charat (i); -             if(Map.containskey (TMP1)) { the                 if(Map.get (TMP1)! =tmp2) -                     return false; -}Else if(Map.containsvalue (TMP2)) { -                 return false; +}Else { - map.put (TMP1, TMP2); +             } A         } at         return true; -     } -}
Solution 2

Use extra space to reduce time complexity.

1  Public classSolution {2      Public Booleanisisomorphic (string s, String t) {3         if(s = =NULL|| t = =NULL)4             return false;5         if(S.length ()! =t.length ())6             return false;7         if(S.length () ==0 && t.length () ==0)8             return true;9         intLength =s.length ();TenMap<character, character> map =NewHashmap<character, character>(); Oneset<character> counts =NewHashset<character>(); A          for(inti = 0; i < length; i++) { -             CharTMP1 =S.charat (i); -             CharTMP2 =T.charat (i); the             if(Map.containskey (TMP1)) { -                 if(Map.get (TMP1)! =tmp2) -                     return false; -}Else if(Counts.contains (TMP2)) { +                 return false; -}Else { + map.put (TMP1, TMP2); A Counts.add (TMP2); at             } -         } -         return true; -     } -}

Isomorphic Strings Solutions

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.