First, the topic
1, examining
2. Analysis
Determines the equal length of the string s and T, where the characters have a one by one corresponding relationship. One of the characters in S can only correspond to a specific character in T.
Second, the answer
1, Ideas:
Method One,
Using a Map, key is the character in S, and value is the character in T.
The process is as follows;
and determine if a key exists in the MAP,
If it does not exist, determine if this value exists;
Returns false if it exists;
Otherwise, put this key-value.
If present, determine if the corresponding value is the same as the character of T,
If yes, continue the traversal;
otherwise, returns false;
Public Booleanisisomorphic (string s, String t) {//1. Replacement charactershashmap<string, string> map =NewHashmap<>(); for(inti = 0; I < s.length (); i++) {String str1= S.substring (I, i + 1); String str2= T.substring (I, i + 1); if(!Map.containskey (STR1)) { //2. If there is duplicate value if(Map.values (). Contains (STR2))return false; Map.put (str1, str2); } Else if(!(Map.get (str1). Equals (STR2))) { return false; } } return true; }
Method Two,
①, the creation of two shaped array M1 and M2, respectively, the characters in s corresponding to the ASCII value corresponding to the subscript, and T in the corresponding ASCII value of the subscript, and the initial value is 0;
②, traversing s and T characters, determine whether the two characters correspond to the array values are equal, if not equal, return false, otherwise assigned to the current subscript.
Note: In order to distinguish between the characters labeled 0, the subscript value stored in the array starts from 1
Public Booleanisisomorphic (string s, String t) {intLen =s.length (); int[] M1 =New int[256]; int[] m2 =New int[256]; for(inti = 0; i < Len; i++) { CharC1 =S.charat (i); CharC2 =T.charat (i); if(M1[C1]! =M2[C2])return false; M1[C1]= i + 1; M2[C2]= i + 1; } return true; }
205. Isomorphic Strings