这题 https://leetcode.com/problems/isomorphic-strings Follow-up:
Now you are given a list of words. Determine if s and any dictionary word are isomorphic.
class Solution {
public Solution(String[] dict) {
// todo
}
public boolean isIsomorphic(String s) {
// todo
}
public boolean isIsomorphic(String s, String t) {
// old impl
}
}
Example:
Input: dict = [“sad”, “dad”, “pop”, “cat”, “dog”, “hat”, “egg”, “add”, “bob”, “mom”, “foo”, “test”]
Output:
isIsomorphic(“did”); // true because ‘did’ and ‘bob’ are isomorphic
isIsomorphic(“tea”); // true because ‘tea’ and ‘dog’ are isomorphic
isIsomorphic(“aaa”); // false
isIsomorphic(“abcde”); // false
The naive approach is to iterate all the words every time
public boolean isIsomorphic(String s) {
for (String word : dict) {
if (isIsomorphic(word, s)) return true;
}
return false;
}
How to optimize?