Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12a84ea

Browse files
feat: add solutions to lc problems: No.1935,1936 (#1844)
* No.1935.Maximum Number of Words You Can Type * No.1936.Add Minimum Number of Rungs
1 parent c6953e5 commit 12a84ea

File tree

16 files changed

+568
-114
lines changed

16 files changed

+568
-114
lines changed

‎solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md‎

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:数组或哈希表**
53+
54+
我们可以用哈希表或者一个长度为 26ドル$ 的数组 $s$ 来记录所有损坏的字母键。
55+
56+
然后,我们遍历字符串 $text$ 中的每个单词 $w,ドル如果 $w$ 中的某个字母 $c$ 在 $s$ 中出现过,那么说明这个单词无法输入,答案不需要加一,否则答案需要加一。
57+
58+
遍历结束后,返回答案即可。
59+
60+
时间复杂度 $O(n),ドル空间复杂度 $O(|\Sigma|),ドル其中 $n$ 是字符串 $text$ 的长度,而 $|\Sigma|$ 是字母表的大小,本题中 $|\Sigma|=26$。
61+
5262
<!-- tabs:start -->
5363

5464
### **Python3**
@@ -58,17 +68,8 @@
5868
```python
5969
class Solution:
6070
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
61-
letters = set(brokenLetters)
62-
res = 0
63-
for word in text.split():
64-
find = False
65-
for letter in letters:
66-
if letter in word:
67-
find = True
68-
break
69-
if not find:
70-
res += 1
71-
return res
71+
s = set(brokenLetters)
72+
return sum(all(c not in s for c in w) for w in text.split())
7273
```
7374

7475
### **Java**
@@ -78,24 +79,129 @@ class Solution:
7879
```java
7980
class Solution {
8081
public int canBeTypedWords(String text, String brokenLetters) {
81-
Set<Character> letters = new HashSet<>();
82+
boolean[] s = new boolean[26];
8283
for (char c : brokenLetters.toCharArray()) {
83-
letters.add(c);
84+
s[c - 'a'] = true;
85+
}
86+
int ans = 0;
87+
for (String w : text.split(" ")) {
88+
for (char c : w.toCharArray()) {
89+
if (s[c - 'a']) {
90+
--ans;
91+
break;
92+
}
93+
}
94+
++ans;
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
int canBeTypedWords(string text, string brokenLetters) {
107+
bool s[26]{};
108+
for (char& c : brokenLetters) {
109+
s[c - 'a'] = true;
84110
}
85-
int res = 0;
86-
for (String word : text.split(" ")) {
87-
boolean find = false;
88-
for (char c : letters) {
89-
if (word.indexOf(c) > -1) {
90-
find = true;
111+
int ans = 0;
112+
for (auto& w : split(text, ' ')) {
113+
for (char& c : w) {
114+
if (s[c - 'a']) {
115+
--ans;
91116
break;
92117
}
93118
}
94-
if (!find) {
95-
++res;
119+
++ans;
120+
}
121+
return ans;
122+
}
123+
124+
vector<string> split(const string& s, char c) {
125+
vector<string> ans;
126+
string t;
127+
for (char d : s) {
128+
if (d == c) {
129+
ans.push_back(t);
130+
t.clear();
131+
} else {
132+
t.push_back(d);
133+
}
134+
}
135+
ans.push_back(t);
136+
return ans;
137+
}
138+
};
139+
```
140+
141+
### **Go**
142+
143+
```go
144+
func canBeTypedWords(text string, brokenLetters string) (ans int) {
145+
s := [26]bool{}
146+
for _, c := range brokenLetters {
147+
s[c-'a'] = true
148+
}
149+
for _, w := range strings.Split(text, " ") {
150+
for _, c := range w {
151+
if s[c-'a'] {
152+
ans--
153+
break
154+
}
155+
}
156+
ans++
157+
}
158+
return
159+
}
160+
```
161+
162+
### **TypeScript**
163+
164+
```ts
165+
function canBeTypedWords(text: string, brokenLetters: string): number {
166+
const s: boolean[] = Array(26).fill(false);
167+
for (const c of brokenLetters) {
168+
s[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
169+
}
170+
let ans = 0;
171+
for (const w of text.split(' ')) {
172+
for (const c of w) {
173+
if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)]) {
174+
--ans;
175+
break;
176+
}
177+
}
178+
++ans;
179+
}
180+
return ans;
181+
}
182+
```
183+
184+
### **Rust**
185+
186+
```rust
187+
impl Solution {
188+
pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 {
189+
let mut s = vec![false; 26];
190+
for c in broken_letters.chars() {
191+
s[c as usize - 'a' as usize] = true;
192+
}
193+
let mut ans = 0;
194+
let words = text.split_whitespace();
195+
for w in words {
196+
for c in w.chars() {
197+
if s[c as usize - 'a' as usize] {
198+
ans -= 1;
199+
break;
200+
}
96201
}
202+
ans += 1;
97203
}
98-
return res;
204+
ans
99205
}
100206
}
101207
```

‎solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md‎

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,49 +46,155 @@
4646

4747
## Solutions
4848

49+
**Method 1: Array or Hash Table**
50+
51+
We can use a hash table or an array $s$ of length 26ドル$ to record all the broken letter keys.
52+
53+
Then, we traverse each word $w$ in the string $text,ドル and if any letter $c$ in $w$ appears in $s,ドル it means that the word cannot be typed, and we do not need to add one to the answer. Otherwise, we need to add one to the answer.
54+
55+
After the traversal, we return the answer.
56+
57+
The time complexity is $O(n),ドル and the space complexity is $O(|\Sigma|),ドル where $n$ is the length of the string $text,ドル and $|\Sigma|$ is the size of the alphabet. In this problem, $|\Sigma|=26$.
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
5262

5363
```python
5464
class Solution:
5565
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
56-
letters = set(brokenLetters)
57-
res = 0
58-
for word in text.split():
59-
find = False
60-
for letter in letters:
61-
if letter in word:
62-
find = True
63-
break
64-
if not find:
65-
res += 1
66-
return res
66+
s = set(brokenLetters)
67+
return sum(all(c not in s for c in w) for w in text.split())
6768
```
6869

6970
### **Java**
7071

7172
```java
7273
class Solution {
7374
public int canBeTypedWords(String text, String brokenLetters) {
74-
Set<Character> letters = new HashSet<>();
75+
boolean[] s = new boolean[26];
7576
for (char c : brokenLetters.toCharArray()) {
76-
letters.add(c);
77+
s[c - 'a'] = true;
78+
}
79+
int ans = 0;
80+
for (String w : text.split(" ")) {
81+
for (char c : w.toCharArray()) {
82+
if (s[c - 'a']) {
83+
--ans;
84+
break;
85+
}
86+
}
87+
++ans;
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int canBeTypedWords(string text, string brokenLetters) {
100+
bool s[26]{};
101+
for (char& c : brokenLetters) {
102+
s[c - 'a'] = true;
77103
}
78-
int res = 0;
79-
for (String word : text.split(" ")) {
80-
boolean find = false;
81-
for (char c : letters) {
82-
if (word.indexOf(c) > -1) {
83-
find = true;
104+
int ans = 0;
105+
for (auto& w : split(text, ' ')) {
106+
for (char& c : w) {
107+
if (s[c - 'a']) {
108+
--ans;
84109
break;
85110
}
86111
}
87-
if (!find) {
88-
++res;
112+
++ans;
113+
}
114+
return ans;
115+
}
116+
117+
vector<string> split(const string& s, char c) {
118+
vector<string> ans;
119+
string t;
120+
for (char d : s) {
121+
if (d == c) {
122+
ans.push_back(t);
123+
t.clear();
124+
} else {
125+
t.push_back(d);
126+
}
127+
}
128+
ans.push_back(t);
129+
return ans;
130+
}
131+
};
132+
```
133+
134+
### **Go**
135+
136+
```go
137+
func canBeTypedWords(text string, brokenLetters string) (ans int) {
138+
s := [26]bool{}
139+
for _, c := range brokenLetters {
140+
s[c-'a'] = true
141+
}
142+
for _, w := range strings.Split(text, " ") {
143+
for _, c := range w {
144+
if s[c-'a'] {
145+
ans--
146+
break
147+
}
148+
}
149+
ans++
150+
}
151+
return
152+
}
153+
```
154+
155+
### **TypeScript**
156+
157+
```ts
158+
function canBeTypedWords(text: string, brokenLetters: string): number {
159+
const s: boolean[] = Array(26).fill(false);
160+
for (const c of brokenLetters) {
161+
s[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
162+
}
163+
let ans = 0;
164+
for (const w of text.split(' ')) {
165+
for (const c of w) {
166+
if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)]) {
167+
--ans;
168+
break;
169+
}
170+
}
171+
++ans;
172+
}
173+
return ans;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
impl Solution {
181+
pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 {
182+
let mut s = vec![false; 26];
183+
for c in broken_letters.chars() {
184+
s[c as usize - 'a' as usize] = true;
185+
}
186+
let mut ans = 0;
187+
let words = text.split_whitespace();
188+
for w in words {
189+
for c in w.chars() {
190+
if s[c as usize - 'a' as usize] {
191+
ans -= 1;
192+
break;
193+
}
89194
}
195+
ans += 1;
90196
}
91-
return res;
197+
ans
92198
}
93199
}
94200
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int canBeTypedWords(string text, string brokenLetters) {
4+
bool s[26]{};
5+
for (char& c : brokenLetters) {
6+
s[c - 'a'] = true;
7+
}
8+
int ans = 0;
9+
for (auto& w : split(text, ' ')) {
10+
for (char& c : w) {
11+
if (s[c - 'a']) {
12+
--ans;
13+
break;
14+
}
15+
}
16+
++ans;
17+
}
18+
return ans;
19+
}
20+
21+
vector<string> split(const string& s, char c) {
22+
vector<string> ans;
23+
string t;
24+
for (char d : s) {
25+
if (d == c) {
26+
ans.push_back(t);
27+
t.clear();
28+
} else {
29+
t.push_back(d);
30+
}
31+
}
32+
ans.push_back(t);
33+
return ans;
34+
}
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /