Skip to content

Commit 75e692f

Browse files
authored
Merge pull request #1314 from 0xff-dev/1935
Add solution and test-cases for problem 1935
2 parents 23fff8f + cd872d9 commit 75e692f

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1935.Maximum Number of Words You Can Type][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
5+
6+
Given a string `text` of words separated by a single space (no leading or trailing spaces) and a string `brokenLetters` of all **distinct** letter keys that are broken, return the **number of words** in `text` you can fully type using this keyboard.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: text = "hello world", brokenLetters = "ad"
12+
Output: 1
13+
Explanation: We cannot type "world" because the 'd' key is broken.
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Number of Words You Can Type
23-
```go
18+
```
19+
Input: text = "leet code", brokenLetters = "lt"
20+
Output: 1
21+
Explanation: We cannot type "leet" because the 'l' and 't' keys are broken.
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: text = "leet code", brokenLetters = "e"
28+
Output: 0
29+
Explanation: We cannot type either word because the 'e' key is broken.
30+
```
2631

2732
## 结语
2833

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(text string, brokenLetters string) int {
6+
broken := [26]bool{}
7+
for _, b := range brokenLetters {
8+
broken[b-'a'] = true
9+
}
10+
ret, i := 0, 0
11+
for _, word := range strings.Split(text, " ") {
12+
for i = 0; i < len(word); i++ {
13+
if broken[word[i]-'a'] {
14+
break
15+
}
16+
}
17+
if i == len(word) {
18+
ret++
19+
}
20+
}
21+
return ret
522
}

leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs string
14+
brokenLetters string
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "hello world", "ad", 1},
18+
{"TestCase2", "leet code", "lt", 1},
19+
{"TestCase3", "leet code", "e", 0},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.brokenLetters)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.brokenLetters)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)