"Leetcode-Interview algorithm classic-java Implementation" "205-isomorphic Strings (isomorphic string)"

Source: Internet
Author: User

"205-isomorphic Strings (Homogeneous string)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original 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.

Main Topic

Given two strings s and T, determine if they are isomorphic. If the string s can get the string T in the way of character substitution, then s and T are isomorphic. Each occurrence of a character must be replaced by its corresponding character, as well as ensuring that the original order has not changed. Two characters cannot be mapped to the same character, but a character can be mapped to itself.

Thinking of solving problems

"As long as s and T meet one-to-one mapping."
Use a hash table map to maintain the mapping of characters in two strings, and save the mapped values with a set. (S[i], t[i]), if the s[i] key does not appear in the map and T[i] does not appear in the set, it is added to the mapping relationship, the t[i] value has been present, the description is a many-to-one mapping, does not conform to return false. S[i] If it has already occurred, set to S[k], the corresponding map value is t[k]), that is s[i]==s[k], then find s[k] of the corresponding value t[k], if T[I]!=T[K], the existence of a single character two different mappings, two strings are not isomorphic, Returns false, continuing with the next character until the end.

Code Implementation

Algorithm implementation class

ImportJava.Util.HashMap;ImportJava.Util.HashSet;ImportJava.Util.Map;ImportJava.Util.Set; PublicClass Solution { PublicBoolean isisomorphic (StringSStringT) {//Two strings are empty        if(s== NULL &&T== NULL) {return true; }//Only one is empty        Else if(s== NULL ||T== NULL) {return false; }//Two string lengths are 0        Else if(s.Length ()== 0 &&T.Length ()== 0) {return true; }///Two strings are not equal in length        Else if(s.Length ()!=T.Length ()) {return false; }//Save Mapping Relationship        Map<Character, Character> Map = NewHashMap<>(s.Length ());Set<Character> Set = NewHashSet<>(t.Length ());        Char Schar;        Char TChar; for (int i= 0; I<S.Length (); I++) {Schar=S.CharAt (i); TChar=T.CharAt (i);//key is not present, the mapping relationship is saved            if(!Map.ContainsKey (Schar)) {if(Set.Contains (TChar)) {return false; }Else{Map.Put (s.CharAt (i), t.CharAt (i));Set.Add (TChar); }            }//If the key has already appeared            Else{The value of the original key mapping is Map.get (Schar), and now the value to be mapped is TCHAR                //If two values are not equal, the description has been mapped two times, does not conform, returns false                if(Map.Get (Schar)!=TChar) {return false; }            }        }return true; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48021425"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "205-isomorphic Strings (isomorphic string)"

Related Article

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.