Skip to content

Commit 30ca3c6

Browse files
authored
Create Ostrichcrab.md
1 parent 5f37829 commit 30ca3c6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2018.11.30-leetcode890/Ostrichcrab.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
```
2+
class Solution {
3+
public List<String> findAndReplacePattern(String[] words, String pattern) {
4+
List<String> ans = new ArrayList();
5+
for (String word: words)
6+
if (match(word, pattern))
7+
ans.add(word);
8+
return ans;
9+
}
10+
11+
public boolean match(String word, String pattern) {
12+
Map<Character, Character> m1 = new HashMap();
13+
Map<Character, Character> m2 = new HashMap();
14+
15+
for (int i = 0; i < word.length(); ++i) {
16+
char w = word.charAt(i);
17+
char p = pattern.charAt(i);
18+
if (!m1.containsKey(w)) m1.put(w, p);
19+
if (!m2.containsKey(p)) m2.put(p, w);
20+
if (m1.get(w) != p || m2.get(p) != w)
21+
return false;
22+
}
23+
24+
return true;
25+
}
26+
}
27+
```

0 commit comments

Comments
 (0)