We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5f37829 commit 30ca3c6Copy full SHA for 30ca3c6
2018.11.30-leetcode890/Ostrichcrab.md
@@ -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