Skip to content

Commit 6fc81b1

Browse files
solves keyboar row in java and python
1 parent 896ec79 commit 6fc81b1

File tree

3 files changed

+119
-4
lines changed

3 files changed

+119
-4
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Leetcode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-106/1412-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-106/1412-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-19/1412-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-107/1412-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-107/1412-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-20/1412-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77

88
## Problems
@@ -129,7 +129,7 @@
129129
| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MaxConsecutiveOnes.java) |
130130
| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConstructTheRectangle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/construct_the_rectangle.py) |
131131
| 496 | [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NextGreaterElementI.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/next_greater_element_i.py) |
132-
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | Easy | |
132+
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/KeyBoardRow.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/keyboard_row.py) |
133133
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | Easy | |
134134
| 504 | [Base 7](https://leetcode.com/problems/base-7) | Easy | |
135135
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks) | Easy | |

Diff for: python/keyboard_row.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def __init__(self):
6+
self.alphabetPosition = {
7+
'q': 1,
8+
'w': 1,
9+
'e': 1,
10+
'r': 1,
11+
't': 1,
12+
'y': 1,
13+
'u': 1,
14+
'i': 1,
15+
'o': 1,
16+
'p': 1,
17+
'a': 2,
18+
's': 2,
19+
'd': 2,
20+
'f': 2,
21+
'g': 2,
22+
'h': 2,
23+
'j': 2,
24+
'k': 2,
25+
'l': 2,
26+
'z': 3,
27+
'x': 3,
28+
'c': 3,
29+
'v': 3,
30+
'b': 3,
31+
'n': 3,
32+
'm': 3,
33+
}
34+
35+
def findWords(self, words: List[str]) -> List[str]:
36+
result = []
37+
for word in words:
38+
if self.singleRowWord(word.lower()):
39+
result.append(word)
40+
return result
41+
42+
def singleRowWord(self, word: str) -> bool:
43+
row = self.alphabetPosition[word[0]]
44+
for index in range(1, len(word)):
45+
if self.alphabetPosition[word[index]] != row:
46+
return False
47+
return True

Diff for: src/KeyBoardRow.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class KeyBoardRow {
7+
private static final Map<Character, Integer> alphabetPosition = new HashMap<>();
8+
9+
static {
10+
alphabetPosition.put('Q', 1);
11+
alphabetPosition.put('W', 1);
12+
alphabetPosition.put('E', 1);
13+
alphabetPosition.put('R', 1);
14+
alphabetPosition.put('T', 1);
15+
alphabetPosition.put('Y', 1);
16+
alphabetPosition.put('U', 1);
17+
alphabetPosition.put('I', 1);
18+
alphabetPosition.put('O', 1);
19+
alphabetPosition.put('P', 1);
20+
21+
alphabetPosition.put('A', 2);
22+
alphabetPosition.put('S', 2);
23+
alphabetPosition.put('D', 2);
24+
alphabetPosition.put('F', 2);
25+
alphabetPosition.put('G', 2);
26+
alphabetPosition.put('H', 2);
27+
alphabetPosition.put('J', 2);
28+
alphabetPosition.put('K', 2);
29+
alphabetPosition.put('L', 2);
30+
31+
alphabetPosition.put('Z', 3);
32+
alphabetPosition.put('X', 3);
33+
alphabetPosition.put('C', 3);
34+
alphabetPosition.put('V', 3);
35+
alphabetPosition.put('B', 3);
36+
alphabetPosition.put('N', 3);
37+
alphabetPosition.put('M', 3);
38+
}
39+
40+
public static String[] findWords(String[] words) {
41+
List<String> result = new ArrayList<>();
42+
for (String word : words) {
43+
if (singleRowWord(word.toUpperCase())) {
44+
result.add(word);
45+
}
46+
}
47+
48+
return arrayListToArray(result);
49+
}
50+
51+
private static String[] arrayListToArray(List<String> list) {
52+
String[] result = new String[list.size()];
53+
for (int index = 0 ; index < result.length ; index++) {
54+
result[index] = list.get(index);
55+
}
56+
return result;
57+
}
58+
59+
private static boolean singleRowWord(String word) {
60+
int row = alphabetPosition.get(word.charAt(0));
61+
for (int index = 1 ; index < word.length() ; index++) {
62+
if (row != alphabetPosition.get(word.charAt(index))) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
}

0 commit comments

Comments
 (0)