[Question]
Given two stringsSAndT, Determine if they are isomorphic.
Two strings are isomorphic if the characters inSCan be replaced to getT.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given"egg"
,"add"
, Return true.
Given"foo"
,"bar"
, Return false.
Given"paper"
,"title"
, Return true.
Given two strings S and T, determine whether they are homogeneous.
If all characters in s can be replaced by characters in T, the two strings are homogeneous.
All the characters that appear must be replaced by another character. No two characters can be mapped to the same character.
Example
To "egg", "add", return true.
To "foo", "bar", return false.
To "paper", "title", return true.
[Analysis]
1. Map the characters of two strings to map
[Algorithm Implementation]
public class Solution { public boolean isIsomorphic(String s, String t) { Map<Character,Character> map = new HashMap<Character,Character>(); //Map<Character,Character> map2 = new HashMap<Character,Character>(); char[] cs = s.toCharArray(); char[] ts = t.toCharArray(); int len = cs.length; for(int i=0; i<len; i++) { if(map.containsKey(cs[i])) { if(map.get(cs[i])!=ts[i]) return false; }else if(map.containsValue(ts[i])){ return false; }else { map.put(cs[i], ts[i]); } } return true; }}
205-ismorphics strings