Given strings
sand
T, determine if they is isomorphic.
The strings is isomorphic if the characters in s can is replaced to gett.
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.
Ideas
Create a map to save the mapping relationship, while maintaining the mapped char with a set, ensuring that the same char is not mapped two times.
[CODE]
public class Solution { //test case: ' Egg ', ' Add ' public Boolean isisomorphic (string s, String t) { //init che CK if (s==null | | t==null) return false; if (s.length () = T.length ()) return false; Map<character, character> map = new Hashmap<character, character> (); set<character> set = new Hashset<character> (); for (int i=0; i<s.length (); i++) { char C1 = S.charat (i); char C2 = T.charat (i); if (Map.containskey (C1)) { if (Map.get (c1)! = C2) return false; } else { if (set.contains (C2)) return False ; else { map.put (c1, C2); Set.add (C2); }}} return true; }}
Leetcode 205:isomorphic Strings