Skip to content

Commit 90c7d0e

Browse files
committed
solve problem Keyboard Row
1 parent 1eb001d commit 90c7d0e

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ All solutions will be accepted!
99
|461|[Hamming Distance](https://leetcode-cn.com/problems/hamming-distance/description/)|[java/py/js](./algorithms/HammingDistance)|Easy|
1010
|476|[Number Complement](https://leetcode-cn.com/problems/number-complement/description/)|[java/py/js](./algorithms/NumberComplement)|Easy|
1111
|728|[Self Dividing Numbers](https://leetcode-cn.com/problems/self-dividing-numbers/description/)|[java/py/js](./algorithms/SelfDividingNumbers)|Easy|
12-
|804|[Unique Morse Code Words](https://leetcode-cn.com/problems/unique-morse-code-words/description/)|[java/py/js](./algorithms/UniqueMorseCodeWords)|Easy|
12+
|804|[Unique Morse Code Words](https://leetcode-cn.com/problems/unique-morse-code-words/description/)|[java/py/js](./algorithms/UniqueMorseCodeWords)|Easy|
13+
|500|[Keyboard Row](https://leetcode-cn.com/problems/keyboard-row/description/)|[java/py/js](./algorithms/KeyboardRow)|Easy|

algorithms/KeyboardRow/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Keyboard Row
2+
we can use prime product to get modulo to solve this problem, but be careful about js's Number overflow!

algorithms/KeyboardRow/Solution.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public String[] findWords(String[] words) {
3+
int[] primeList = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 };
4+
long firstLineProduct = 1L;
5+
long secondLineProduct = 1L;
6+
long lastLineProduct = 1L;
7+
for(char c : "qwertyuiop".toCharArray()) {
8+
firstLineProduct *= primeList[(int) c - (int) 'a'];
9+
}
10+
for(char c : "asdfghjkl".toCharArray()) {
11+
secondLineProduct *= primeList[(int) c - (int) 'a'];
12+
}
13+
for(char c : "zxcvbnm".toCharArray()) {
14+
lastLineProduct *= primeList[(int) c - (int) 'a'];
15+
}
16+
List<String> result = new ArrayList<String>();
17+
18+
for (String word : words) {
19+
String unduplicatedWord = "";
20+
for (String c : word.toLowerCase().split("")) {
21+
if (!unduplicatedWord.contains(c)) {
22+
unduplicatedWord += c;
23+
}
24+
}
25+
long product = 1L;
26+
for(char c : unduplicatedWord.toCharArray()) {
27+
product *= primeList[(int) c - (int) 'a'];
28+
}
29+
30+
if (firstLineProduct % product == 0 || secondLineProduct % product == 0 || lastLineProduct % product == 0) {
31+
result.add(word);
32+
}
33+
}
34+
return result.toArray(new String[result.size()]);
35+
}
36+
}

algorithms/KeyboardRow/solution.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {string[]}
4+
*/
5+
var findWords = function(words) {
6+
// js didn't support Long Integer, So we use another way to solve problem
7+
let firstLine = 'qwertyuiop',
8+
secondLine = 'asdfghjkl',
9+
lastLine = 'zxcvbnm'
10+
let array = []
11+
words.forEach(word => {
12+
let first = second = last = 0
13+
Array.from(new Set(word.toLowerCase().split(''))).forEach(char => {
14+
if (firstLine.indexOf(char) !== -1) first = 1
15+
else if (secondLine.indexOf(char) !== -1) second = 1
16+
else if (lastLine.indexOf(char) !== -1) last = 1
17+
})
18+
if (first + second + last === 1) {
19+
array.push(word)
20+
}
21+
})
22+
return array
23+
};

algorithms/KeyboardRow/solution.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def findWords(self, words):
3+
"""
4+
:type words: List[str]
5+
:rtype: List[str]
6+
"""
7+
# convert to division
8+
prime_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
9+
first_line_product = reduce(lambda x, y : x * y, [prime_list[ord(x) - ord('a')] for x in list('qwertyuiop')])
10+
second_line_product = reduce(lambda x, y : x * y, [prime_list[ord(x) - ord('a')] for x in list('asdfghjkl')])
11+
last_line_product = reduce(lambda x, y : x * y, [prime_list[ord(x) - ord('a')] for x in list('zxcvbnm')])
12+
13+
result_list = []
14+
for word in words:
15+
product = reduce(lambda x, y : x * y, [prime_list[ord(x) - ord('a')] for x in set(word.lower())])
16+
if first_line_product % product == 0 or second_line_product % product == 0 or last_line_product % product == 0:
17+
result_list.append(word)
18+
return result_list
19+

0 commit comments

Comments
 (0)