From cd872d96faa14e1046043a7cc5f0f3d099ae2d28 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 15 Sep 2025 08:51:47 +0800 Subject: [PATCH] Add solution and test-cases for problem 1935 --- .../README.md | 31 +++++++++++-------- .../Solution.go | 21 +++++++++++-- .../Solution_test.go | 23 +++++++------- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/README.md b/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/README.md index d2d12e7dd..c046d502a 100755 --- a/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/README.md +++ b/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/README.md @@ -1,28 +1,33 @@ # [1935.Maximum Number of Words You Can Type][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly. + +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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: text = "hello world", brokenLetters = "ad" +Output: 1 +Explanation: We cannot type "world" because the 'd' key is broken. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Maximum Number of Words You Can Type -```go +``` +Input: text = "leet code", brokenLetters = "lt" +Output: 1 +Explanation: We cannot type "leet" because the 'l' and 't' keys are broken. ``` +**Example 3:** + +``` +Input: text = "leet code", brokenLetters = "e" +Output: 0 +Explanation: We cannot type either word because the 'e' key is broken. +``` ## 结语 diff --git a/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution.go b/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution.go index d115ccf5e..b3318beb1 100644 --- a/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution.go +++ b/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution.go @@ -1,5 +1,22 @@ package Solution -func Solution(x bool) bool { - return x +import "strings" + +func Solution(text string, brokenLetters string) int { + broken := [26]bool{} + for _, b := range brokenLetters { + broken[b-'a'] = true + } + ret, i := 0, 0 + for _, word := range strings.Split(text, " ") { + for i = 0; i < len(word); i++ { + if broken[word[i]-'a'] { + break + } + } + if i == len(word) { + ret++ + } + } + return ret } diff --git a/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution_test.go b/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution_test.go index 14ff50eb4..a31580733 100644 --- a/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution_test.go +++ b/leetcode/1901-2000/1935.Maximum-Number-of-Words-You-Can-Type/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + inputs string + brokenLetters string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "hello world", "ad", 1}, + {"TestCase2", "leet code", "lt", 1}, + {"TestCase3", "leet code", "e", 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.brokenLetters) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.brokenLetters) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }