Skip to content

Commit 68e6a90

Browse files
committed
feat: add solutions to lc problem: No.0500
No.0500.Keyboard Row
1 parent 7cc171f commit 68e6a90

File tree

5 files changed

+147
-16
lines changed

5 files changed

+147
-16
lines changed

solution/0500-0599/0500.Keyboard Row/README.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
<li><code>words[i]</code> 由英文字母(小写和大写字母)组成</li>
5252
</ul>
5353

54-
5554
## 解法
5655

5756
<!-- 这里可写通用的实现逻辑 -->
@@ -121,6 +120,63 @@ class Solution {
121120
}
122121
```
123122

123+
```java
124+
class Solution {
125+
public String[] findWords(String[] words) {
126+
String s = "12210111011122000010020202";
127+
List<String> res = new ArrayList<>();
128+
for (String word : words) {
129+
Set<Character> t = new HashSet<>();
130+
for (char c : word.toLowerCase().toCharArray()) {
131+
t.add(s.charAt(c - 'a'));
132+
}
133+
if (t.size() == 1) {
134+
res.add(word);
135+
}
136+
}
137+
return res.toArray(new String[0]);
138+
}
139+
}
140+
```
141+
142+
### **C++**
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
vector<string> findWords(vector<string>& words) {
148+
string s = "12210111011122000010020202";
149+
vector<string> ans;
150+
for (auto& word : words)
151+
{
152+
unordered_set<char> t;
153+
for (char c : word) t.insert(s[tolower(c) - 'a']);
154+
if (t.size() == 1) ans.push_back(word);
155+
}
156+
return ans;
157+
}
158+
};
159+
```
160+
161+
### **Go**
162+
163+
```go
164+
func findWords(words []string) []string {
165+
s := "12210111011122000010020202"
166+
var ans []string
167+
for _, word := range words {
168+
t := make(map[byte]bool)
169+
for _, c := range word {
170+
t[s[unicode.ToLower(c)-'a']] = true
171+
}
172+
if len(t) == 1 {
173+
ans = append(ans, word)
174+
}
175+
}
176+
return ans
177+
}
178+
```
179+
124180
### **...**
125181

126182
```

solution/0500-0599/0500.Keyboard Row/README_EN.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<li><code>words[i]</code> consists of English letters (both lowercase and uppercase).&nbsp;</li>
4646
</ul>
4747

48-
4948
## Solutions
5049

5150
<!-- tabs:start -->
@@ -96,6 +95,63 @@ class Solution {
9695
}
9796
```
9897

98+
```java
99+
class Solution {
100+
public String[] findWords(String[] words) {
101+
String s = "12210111011122000010020202";
102+
List<String> res = new ArrayList<>();
103+
for (String word : words) {
104+
Set<Character> t = new HashSet<>();
105+
for (char c : word.toLowerCase().toCharArray()) {
106+
t.add(s.charAt(c - 'a'));
107+
}
108+
if (t.size() == 1) {
109+
res.add(word);
110+
}
111+
}
112+
return res.toArray(new String[0]);
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
vector<string> findWords(vector<string>& words) {
123+
string s = "12210111011122000010020202";
124+
vector<string> ans;
125+
for (auto& word : words)
126+
{
127+
unordered_set<char> t;
128+
for (char c : word) t.insert(s[tolower(c) - 'a']);
129+
if (t.size() == 1) ans.push_back(word);
130+
}
131+
return ans;
132+
}
133+
};
134+
```
135+
136+
### **Go**
137+
138+
```go
139+
func findWords(words []string) []string {
140+
s := "12210111011122000010020202"
141+
var ans []string
142+
for _, word := range words {
143+
t := make(map[byte]bool)
144+
for _, c := range word {
145+
t[s[unicode.ToLower(c)-'a']] = true
146+
}
147+
if len(t) == 1 {
148+
ans = append(ans, word)
149+
}
150+
}
151+
return ans
152+
}
153+
```
154+
99155
### **...**
100156

101157
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<string> findWords(vector<string>& words) {
4+
string s = "12210111011122000010020202";
5+
vector<string> ans;
6+
for (auto& word : words)
7+
{
8+
unordered_set<char> t;
9+
for (char c : word) t.insert(s[tolower(c) - 'a']);
10+
if (t.size() == 1) ans.push_back(word);
11+
}
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func findWords(words []string) []string {
2+
s := "12210111011122000010020202"
3+
var ans []string
4+
for _, word := range words {
5+
t := make(map[byte]bool)
6+
for _, c := range word {
7+
t[s[unicode.ToLower(c)-'a']] = true
8+
}
9+
if len(t) == 1 {
10+
ans = append(ans, word)
11+
}
12+
}
13+
return ans
14+
}

solution/0500-0599/0500.Keyboard Row/Solution.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
class Solution {
22
public String[] findWords(String[] words) {
3-
String s1 = "qwertyuiopQWERTYUIOP";
4-
String s2 = "asdfghjklASDFGHJKL";
5-
String s3 = "zxcvbnmZXCVBNM";
3+
String s = "12210111011122000010020202";
64
List<String> res = new ArrayList<>();
75
for (String word : words) {
8-
int n1 = 0, n2 = 0, n3 = 0;
9-
int n = word.length();
10-
for (int i = 0; i < n; ++i) {
11-
if (s1.contains(String.valueOf(word.charAt(i)))) {
12-
++n1;
13-
} else if (s2.contains(String.valueOf(word.charAt(i)))) {
14-
++n2;
15-
} else {
16-
++n3;
17-
}
6+
Set<Character> t = new HashSet<>();
7+
for (char c : word.toLowerCase().toCharArray()) {
8+
t.add(s.charAt(c - 'a'));
189
}
19-
if (n1 == n || n2 == n || n3 == n) {
10+
if (t.size() == 1) {
2011
res.add(word);
2112
}
2213
}

0 commit comments

Comments
 (0)