Problem
Given two (dictionary) words as Strings, determine if they are isomorphic.Two words are called isomorphic if the letters in one word can be remapped to get the second word.
Remapping a letter means replacing all occurrences of it with another letter while the ordering of the letters remains unchanged.
No two letters may map to the same letter, but a letter may map to itself.
Example:
given "foo", "app"; returns true we can map f -> a and o->p
given "bar", "foo"; returns false we can't map both 'a' and 'r' to 'o'
given "ab", "ca"; returns true we can map 'a' -> 'b' and 'c' -> 'a'
最直接的方法就是见两个HashMap维护住字符的对应关系,O(n)
public static boolean check(String s,String t){
if(s.length()!=t.length()) return false;
HashMap<Character,Character> map1=new HashMap<Character, Character>();
HashMap<Character,Character> map2=new HashMap<Character, Character>();
for(int i=0;i<s.length();i++){
char c1=s.charAt(i);
char c2=t.charAt(i);
if(map1.containsKey(c1)){
if(map1.get(c1)!=c2) return false;
}
if(map2.containsKey(c2)){
if(map2.get(c2)!=c1) return false;
}
map1.put(c1, c2);
map2.put(c2, c1);
}
return true;
}
本文介绍了一种通过使用两个HashMap来判断两个字符串是否为同构字符串的方法。此方法的时间复杂度为O(n),适用于需要判断两个字符串是否可以通过字符映射相互转换的场景。
284

被折叠的 条评论
为什么被折叠?



