Description
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
Example Given "egg"
, "add"
, return true.
Given "foo"
, "bar"
, return false.
Given "paper"
, "title"
, return true.
public boolean isIsomorphic(String s, String t) {
if(s.length()!=t.length()){
return false;
}
HashMap<Character,Character> map=new HashMap<>();
for(int i=0;i<s.length();i++){
if(map.containsKey(s.charAt(i))){
if((char)(map.get(s.charAt(i)))!=t.charAt(i))
return false;
}
else{
if(map.containsValue(t.charAt(i)))
return false;
}
map.put(s.charAt(i),t.charAt(i));
}
return true;
}