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.
A hash table is used to record the mapping of characters, as long as the 1-to-1 mapping is guaranteed.
public class Solution {
public Boolean isisomorphic (string s, String t) {
Map<character, character> map1 = new hashmap<> ();
Map<character, character> map2 = new hashmap<> ();
for (int i = 0; i < s.length (); i++) {
char a = S.charat (i);
Char B = T.charat (i);
if (Map1.containskey (a) && map1.get (a)! = b) {
return false;
}
if (Map2.containskey (b) && map2.get (b)! = a) {
return false;
}
Map1.put (A, b);
Map2.put (b, a);
}
return true;
}
}
205. Isomorphic Strings