Topic links
https://leetcode.com/problems/isomorphic-strings/
Original title
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.
Topic translation
Given two strings, determine if they are isomorphic. If a character in a string s can be substituted for another character, resulting in a second string of T, then two strings are isomorphic.
All the same characters in the string are replaced and are kept in the original order. Two different characters cannot be replaced by the same character, and a character can be replaced by its own.
Like what:
Given "egg" , "add" that returns true
Given "foo" , "bar" that returns false
Given "paper" , "title" that returns true
Note: Assume that the length of the two string s and T is the same.
Method of Thinking
Idea one
Traversing the S and T, the character mappings of S to T are stored in the dict, and the traversal process can return FALSE if the mapping of a location is found to be in conflict with the identified mapping. However, this process cannot find the "different characters mapped to the same character", so it is also necessary to determine whether the resulting mapping relationship is duplicated.
Code
class solution(object): def isisomorphic(self, S, T): "" : Type S:str:type t:str:rtype:bool "" "HashMap = {} forIinchXrange (Len (s)):ifS[i] not inchHashmap:hashmap[s[i]] = T[i]elifHashmap[s[i]]! = T[i]:return FalseMapval = [Hashmap[k] forKinchHashMapreturnLen (mapval) = = Len (set (Mapval))
Idea two
Similar to the above idea, in order to avoid the last value in the HashMap "Do not repeat" judgment, when traversing s and T, the mapped values are stored in the Mapval this dict, so that in the middle of the discovery of duplicates can also return false in a timely manner.
Code
class solution(object): def isisomorphic(self, S, T): "" : Type S:str:type t:str:rtype:bool "" "HashMap = {} Mapval = {} forIinchXrange (Len (s)):ifS[i]inchHashMapifHashmap[s[i]]! = T[i]:return False elifT[i]inchMapval:return False Else: hashmap[s[i]] = t[i] mapval[t[i] =True return True
Three Ideas
For S and T, use an array to record the position of each character in the last occurrence of the string, respectively. When both S and T are traversed, false is returned if they are found to have a different position at the last occurrence of the character.
Code
class solution(object): def isisomorphic(self, S, T): "" : Type S:str:type t:str:rtype:bool "" "POS1, Pos2 = [-1]* the, [-1]* the forIinchXrange (Len (s)):ifPos1[ord (S[i])]! = Pos2[ord (T[i]):return FalsePos1[ord (S[i])] = Pos2[ord (T[i])] = ireturn True
Idea four
Similar to the idea of three, equivalent to the idea of three records where the last occurrence, and now record the first occurrence of the position.
For S, the traversal records the position where each character first appears as a new array, and the same treatment for T. If the resulting two arrays are the same, then this is a conforming mapping, s and t isomorphic; otherwise different.
Python's built-in function map and the Find function of the string can be implemented in one line.
Code
class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ return map(s.find, s) == map(t.find, t)
Idea Five
According to the mapping requirements described in the topic, S has how many different characters, T also how many different characters. If we write the mapping in the form of a pair of characters, such as (' A ', ' C ') that the character ' a ' in S is mapped to ' C ' in T, then the number of mappings is the same as that of the characters in S.
With Python's built-in function zip, you can also implement a single line to determine isomorphism.
Code
class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ return len(set(zip(s, t))) == len(set(s)) == len(set(t))
PS: Novice Brush Leetcode, new handwritten blog, write wrong or write not clear also please help point out, thank you!
Reprint Please specify: http://blog.csdn.net/coder_orz/article/details/51681396
205. Isomorphic Strings [Easy] (Python)