Topic:
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.
Idea: Finally a simple, read the question can see, with map to save the map, but also to determine whether the value has been mapped, filter with Set
Code:
Public classSolution { Public Booleanisisomorphic (string s, String t) {Map<character, character> map =NewHashmap<character, character>(); Set<Character> set =NewHashset<character>(); StringBuilder SB=NewStringBuilder (); for(inti = 0; i < Len; i++){ if(!Map.containskey (S.charat (i))) { if(!set.contains (T.charat (i))) {//Filter Value TMap.put (S.charat (i), T.charat (i));//T-SSet.add (T.charat (i)); } Else return false; } sb.append (Map.get (S.charat (i))); } returnsb.tostring (). Equals (t); }}
[Leetcode-java] Isomorphic Strings