Skip to content

Commit 69c2618

Browse files
committed
feat: add solutions to lc problem: No.0500
No.0500.Keyboard Row
1 parent c5b3433 commit 69c2618

File tree

7 files changed

+230
-153
lines changed

7 files changed

+230
-153
lines changed

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

+86-68
Original file line numberDiff line numberDiff line change
@@ -55,86 +55,68 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:字符映射**
59+
60+
我们将每个键盘行的字符映射到对应的行数,然后遍历字符串数组,判断每个字符串是否都在同一行即可。
61+
62+
时间复杂度 $O(L)$,空间复杂度 $O(C)$。其中 $L$ 为所有字符串的长度之和;而 $C$ 为字符集的大小,本题中 $C = 26$。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
6167

6268
<!-- 这里可写当前语言的特殊实现逻辑 -->
6369

64-
用三个集合存储键盘每一行字母。
65-
66-
遍历单词列表 `words`,判断每个单词 `word` 转 set 后是否被以上三个集合其中之一包含,若是,将单词添加到结果数组。
67-
6870
```python
6971
class Solution:
7072
def findWords(self, words: List[str]) -> List[str]:
7173
s1 = set('qwertyuiop')
7274
s2 = set('asdfghjkl')
7375
s3 = set('zxcvbnm')
74-
res = []
75-
for word in words:
76-
t = set(word.lower())
77-
if t <= s1 or t <= s2 or t <= s3:
78-
# 利用 python set 比较
79-
res.append(word)
80-
return res
76+
ans = []
77+
for w in words:
78+
s = set(w.lower())
79+
if s <= s1 or s <= s2 or s <= s3:
80+
ans.append(w)
81+
return ans
82+
```
83+
84+
```python
85+
class Solution:
86+
def findWords(self, words: List[str]) -> List[str]:
87+
ans = []
88+
s = "12210111011122000010020202"
89+
for w in words:
90+
x = s[ord(w[0].lower()) - ord('a')]
91+
if all(s[ord(c.lower()) - ord('a')] == x for c in w):
92+
ans.append(w)
93+
return ans
8194
```
8295

8396
### **Java**
8497

8598
<!-- 这里可写当前语言的特殊实现逻辑 -->
8699

87-
用三个字符串存储键盘每一行字母。
88-
89-
遍历单词列表 `words`,对于每个单词 `word`
90-
91-
1. 分别设置三个计数器,存储当前单词在对应键盘字符串的字母个数;
92-
2. 遍历 `word` 中的每个字母,如果在对应的键盘字符串中,则对应的计数器加 1;
93-
3. 单词遍历结束后,判断是否存在一个计数器值与 `word` 长度相同。如果有,说明该单词所有字母都在同一个键盘字符串中,将单词添加到结果数组。
94-
95-
```java
96-
class Solution {
97-
public String[] findWords(String[] words) {
98-
String s1 = "qwertyuiopQWERTYUIOP";
99-
String s2 = "asdfghjklASDFGHJKL";
100-
String s3 = "zxcvbnmZXCVBNM";
101-
List<String> res = new ArrayList<>();
102-
for (String word : words) {
103-
int n1 = 0, n2 = 0, n3 = 0;
104-
int n = word.length();
105-
for (int i = 0; i < n; ++i) {
106-
if (s1.contains(String.valueOf(word.charAt(i)))) {
107-
++n1;
108-
} else if (s2.contains(String.valueOf(word.charAt(i)))) {
109-
++n2;
110-
} else {
111-
++n3;
112-
}
113-
}
114-
if (n1 == n || n2 == n || n3 == n) {
115-
res.add(word);
116-
}
117-
}
118-
return res.toArray(new String[0]);
119-
}
120-
}
121-
```
122-
123100
```java
124101
class Solution {
125102
public String[] findWords(String[] words) {
126103
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'));
104+
List<String> ans = new ArrayList<>();
105+
for (var w : words) {
106+
String t = w.toLowerCase();
107+
char x = s.charAt(t.charAt(0) - 'a');
108+
boolean ok = true;
109+
for (char c : t.toCharArray()) {
110+
if (s.charAt(c - 'a') != x) {
111+
ok = false;
112+
break;
113+
}
132114
}
133-
if (t.size() == 1) {
134-
res.add(word);
115+
if (ok) {
116+
ans.add(w);
135117
}
136118
}
137-
return res.toArray(new String[0]);
119+
return ans.toArray(new String[0]);
138120
}
139121
}
140122
```
@@ -147,10 +129,18 @@ public:
147129
vector<string> findWords(vector<string>& words) {
148130
string s = "12210111011122000010020202";
149131
vector<string> ans;
150-
for (auto& word : words) {
151-
unordered_set<char> t;
152-
for (char c : word) t.insert(s[tolower(c) - 'a']);
153-
if (t.size() == 1) ans.push_back(word);
132+
for (auto& w : words) {
133+
char x = s[tolower(w[0]) - 'a'];
134+
bool ok = true;
135+
for (char& c : w) {
136+
if (s[tolower(c) - 'a'] != x) {
137+
ok = false;
138+
break;
139+
}
140+
}
141+
if (ok) {
142+
ans.emplace_back(w);
143+
}
154144
}
155145
return ans;
156146
}
@@ -160,19 +150,47 @@ public:
160150
### **Go**
161151
162152
```go
163-
func findWords(words []string) []string {
153+
func findWords(words []string) (ans []string) {
164154
s := "12210111011122000010020202"
165-
var ans []string
166-
for _, word := range words {
167-
t := make(map[byte]bool)
168-
for _, c := range word {
169-
t[s[unicode.ToLower(c)-'a']] = true
155+
for _, w := range words {
156+
x := s[unicode.ToLower(rune(w[0]))-'a']
157+
ok := true
158+
for _, c := range w[1:] {
159+
if s[unicode.ToLower(c)-'a'] != x {
160+
ok = false
161+
break
162+
}
170163
}
171-
if len(t) == 1 {
172-
ans = append(ans, word)
164+
if ok {
165+
ans = append(ans, w)
173166
}
174167
}
175-
return ans
168+
return
169+
}
170+
```
171+
172+
### **C#**
173+
174+
```cs
175+
public class Solution {
176+
public string[] FindWords(string[] words) {
177+
string s = "12210111011122000010020202";
178+
IList<string> ans = new List<string>();
179+
foreach (string w in words) {
180+
char x = s[char.ToLower(w[0]) - 'a'];
181+
bool ok = true;
182+
for (int i = 1; i < w.Length; ++i) {
183+
if (s[char.ToLower(w[i]) - 'a'] != x) {
184+
ok = false;
185+
break;
186+
}
187+
}
188+
if (ok) {
189+
ans.Add(w);
190+
}
191+
}
192+
return ans.ToArray();
193+
}
176194
}
177195
```
178196

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

+80-55
Original file line numberDiff line numberDiff line change
@@ -57,59 +57,48 @@ class Solution:
5757
s1 = set('qwertyuiop')
5858
s2 = set('asdfghjkl')
5959
s3 = set('zxcvbnm')
60-
res = []
61-
for word in words:
62-
t = set(word.lower())
63-
if t <= s1 or t <= s2 or t <= s3:
64-
res.append(word)
65-
return res
60+
ans = []
61+
for w in words:
62+
s = set(w.lower())
63+
if s <= s1 or s <= s2 or s <= s3:
64+
ans.append(w)
65+
return ans
6666
```
6767

68-
### **Java**
69-
70-
```java
71-
class Solution {
72-
public String[] findWords(String[] words) {
73-
String s1 = "qwertyuiopQWERTYUIOP";
74-
String s2 = "asdfghjklASDFGHJKL";
75-
String s3 = "zxcvbnmZXCVBNM";
76-
List<String> res = new ArrayList<>();
77-
for (String word : words) {
78-
int n1 = 0, n2 = 0, n3 = 0;
79-
int n = word.length();
80-
for (int i = 0; i < n; ++i) {
81-
if (s1.contains(String.valueOf(word.charAt(i)))) {
82-
++n1;
83-
} else if (s2.contains(String.valueOf(word.charAt(i)))) {
84-
++n2;
85-
} else {
86-
++n3;
87-
}
88-
}
89-
if (n1 == n || n2 == n || n3 == n) {
90-
res.add(word);
91-
}
92-
}
93-
return res.toArray(new String[0]);
94-
}
95-
}
68+
```python
69+
class Solution:
70+
def findWords(self, words: List[str]) -> List[str]:
71+
ans = []
72+
s = "12210111011122000010020202"
73+
for w in words:
74+
x = s[ord(w[0].lower()) - ord('a')]
75+
if all(s[ord(c.lower()) - ord('a')] == x for c in w):
76+
ans.append(w)
77+
return ans
9678
```
9779

80+
### **Java**
81+
9882
```java
9983
class Solution {
10084
public String[] findWords(String[] words) {
10185
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'));
86+
List<String> ans = new ArrayList<>();
87+
for (var w : words) {
88+
String t = w.toLowerCase();
89+
char x = s.charAt(t.charAt(0) - 'a');
90+
boolean ok = true;
91+
for (char c : t.toCharArray()) {
92+
if (s.charAt(c - 'a') != x) {
93+
ok = false;
94+
break;
95+
}
10796
}
108-
if (t.size() == 1) {
109-
res.add(word);
97+
if (ok) {
98+
ans.add(w);
11099
}
111100
}
112-
return res.toArray(new String[0]);
101+
return ans.toArray(new String[0]);
113102
}
114103
}
115104
```
@@ -122,10 +111,18 @@ public:
122111
vector<string> findWords(vector<string>& words) {
123112
string s = "12210111011122000010020202";
124113
vector<string> ans;
125-
for (auto& word : words) {
126-
unordered_set<char> t;
127-
for (char c : word) t.insert(s[tolower(c) - 'a']);
128-
if (t.size() == 1) ans.push_back(word);
114+
for (auto& w : words) {
115+
char x = s[tolower(w[0]) - 'a'];
116+
bool ok = true;
117+
for (char& c : w) {
118+
if (s[tolower(c) - 'a'] != x) {
119+
ok = false;
120+
break;
121+
}
122+
}
123+
if (ok) {
124+
ans.emplace_back(w);
125+
}
129126
}
130127
return ans;
131128
}
@@ -135,19 +132,47 @@ public:
135132
### **Go**
136133
137134
```go
138-
func findWords(words []string) []string {
135+
func findWords(words []string) (ans []string) {
139136
s := "12210111011122000010020202"
140-
var ans []string
141-
for _, word := range words {
142-
t := make(map[byte]bool)
143-
for _, c := range word {
144-
t[s[unicode.ToLower(c)-'a']] = true
137+
for _, w := range words {
138+
x := s[unicode.ToLower(rune(w[0]))-'a']
139+
ok := true
140+
for _, c := range w[1:] {
141+
if s[unicode.ToLower(c)-'a'] != x {
142+
ok = false
143+
break
144+
}
145145
}
146-
if len(t) == 1 {
147-
ans = append(ans, word)
146+
if ok {
147+
ans = append(ans, w)
148148
}
149149
}
150-
return ans
150+
return
151+
}
152+
```
153+
154+
### **C#**
155+
156+
```cs
157+
public class Solution {
158+
public string[] FindWords(string[] words) {
159+
string s = "12210111011122000010020202";
160+
IList<string> ans = new List<string>();
161+
foreach (string w in words) {
162+
char x = s[char.ToLower(w[0]) - 'a'];
163+
bool ok = true;
164+
for (int i = 1; i < w.Length; ++i) {
165+
if (s[char.ToLower(w[i]) - 'a'] != x) {
166+
ok = false;
167+
break;
168+
}
169+
}
170+
if (ok) {
171+
ans.Add(w);
172+
}
173+
}
174+
return ans.ToArray();
175+
}
151176
}
152177
```
153178

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ class Solution {
33
vector<string> findWords(vector<string>& words) {
44
string s = "12210111011122000010020202";
55
vector<string> ans;
6-
for (auto& word : words) {
7-
unordered_set<char> t;
8-
for (char c : word) t.insert(s[tolower(c) - 'a']);
9-
if (t.size() == 1) ans.push_back(word);
6+
for (auto& w : words) {
7+
char x = s[tolower(w[0]) - 'a'];
8+
bool ok = true;
9+
for (char& c : w) {
10+
if (s[tolower(c) - 'a'] != x) {
11+
ok = false;
12+
break;
13+
}
14+
}
15+
if (ok) {
16+
ans.emplace_back(w);
17+
}
1018
}
1119
return ans;
1220
}

0 commit comments

Comments
 (0)