Skip to content

Commit 905f4e3

Browse files
committed
solve problem Find And Replace Pattern
1 parent 6d2f485 commit 905f4e3

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ All solutions will be accepted!
288288
|129|[Sum Root To Leaf Numbers](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/description/)|[java/py/js](./algorithms/SumRootToLeafNumbers)|Medium|
289289
|537|[Complex Number Multiplication](https://leetcode-cn.com/problems/complex-number-multiplication/description/)|[java/py/js](./algorithms/ComplexNumberMultiplication)|Medium|
290290
|228|[Summary Ranges](https://leetcode-cn.com/problems/summary-ranges/description/)|[java/py/js](./algorithms/SummaryRanges)|Medium|
291+
|890|[Find And Replace Pattern](https://leetcode-cn.com/problems/find-and-replace-pattern/description/)|[java/py/js](./algorithms/FindAndReplacePattern)|Medium|
291292

292293
# Database
293294
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Find And Replace Pattern
2+
This problem is easy to solve by hashmap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public List<String> findAndReplacePattern(String[] words, String pattern) {
3+
int length = pattern.length();
4+
List<String> res = new ArrayList<String>();
5+
6+
for (String word : words) {
7+
Map<Character, Character> charMap = new HashMap<Character, Character>(),
8+
charReverseMap = new HashMap<Character, Character>();
9+
boolean notMatch = false;
10+
11+
for (int i = 0; i < length; i++) {
12+
char char1 = word.charAt(i),
13+
char2 = pattern.charAt(i);
14+
if (charMap.get(char1) == null && charReverseMap.get(char2) == null) {
15+
charMap.put(char1, char2);
16+
charReverseMap.put(char2, char1);
17+
} else if (charMap.get(char1) == null || !charMap.get(char1).equals(char2)) {
18+
notMatch = true;
19+
break;
20+
}
21+
}
22+
23+
if (!notMatch)
24+
res.add(word);
25+
}
26+
27+
return res;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string} pattern
4+
* @return {string[]}
5+
*/
6+
var findAndReplacePattern = function(words, pattern) {
7+
let res = [],
8+
length = pattern.length
9+
10+
words.forEach(word => {
11+
let charMap = {},
12+
charReverseMap = {},
13+
notMatch = false
14+
15+
for (let i = 0; i < length; i++) {
16+
if (!charMap[word[i]] && !charReverseMap[pattern[i]]) {
17+
charMap[word[i]] = pattern[i]
18+
charReverseMap[pattern[i]] = word[i]
19+
} else if (charMap[word[i]] != pattern[i]) {
20+
notMatch = true
21+
break
22+
}
23+
}
24+
if (!notMatch)
25+
res.push(word)
26+
})
27+
28+
return res
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def findAndReplacePattern(self, words, pattern):
3+
"""
4+
:type words: List[str]
5+
:type pattern: str
6+
:rtype: List[str]
7+
"""
8+
res = []
9+
word_length = len(pattern)
10+
11+
for word in words:
12+
char_map = {}
13+
char_reverse_map = {}
14+
not_match = False
15+
16+
for i in xrange(word_length):
17+
if not char_map.get(word[i]) and not char_reverse_map.get(pattern[i]):
18+
char_map[word[i]] = pattern[i]
19+
char_reverse_map[pattern[i]] = word[i]
20+
elif char_map.get(word[i]) != pattern[i]:
21+
# same as char_reverse_map.get(pattern[i]) != word[i]
22+
not_match = True
23+
break
24+
25+
if not not_match:
26+
res.append(word)
27+
28+
return res

0 commit comments

Comments
 (0)