From 91950bf163563a7153ddca45f24865ca6a17a108 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 15 Jul 2025 08:58:45 +0800 Subject: [PATCH] Add solution and test-cases for problem 3136 --- leetcode/3101-3200/3136.Valid-Word/README.md | 54 ++++++++++++++----- .../3101-3200/3136.Valid-Word/Solution.go | 27 +++++++++- .../3136.Valid-Word/Solution_test.go | 12 ++--- 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/leetcode/3101-3200/3136.Valid-Word/README.md b/leetcode/3101-3200/3136.Valid-Word/README.md index 8b87e732b..a79e323bf 100755 --- a/leetcode/3101-3200/3136.Valid-Word/README.md +++ b/leetcode/3101-3200/3136.Valid-Word/README.md @@ -1,28 +1,58 @@ # [3136.Valid Word][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 +A word is considered **valid** if: + +- It contains a **minimum** of 3 characters. +- It contains only digits (0-9), and English letters (uppercase and lowercase). +- It includes **at least** one **vowel**. +- It includes **at least** one **consonant**. + +You are given a string `word`. + +Return `true` if `word` is valid, otherwise, return `false`. + +**Notes**: + +- `'a'`, `'e'`, `'i'`, `'o'`, `'u'`, and their uppercases are **vowels**. +- A **consonant** is an English letter that is not a vowel. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: word = "234Adas" + +Output: true + +Explanation: + +This word satisfies the conditions. ``` -## 题意 -> ... +**Example 2:** -## 题解 +``` +Input: word = "b3" + +Output: false + +Explanation: -### 思路1 -> ... -Valid Word -```go +The length of this word is fewer than 3, and does not have a vowel. ``` +**Example 3:** + +``` +Input: word = "a3$e" + +Output: false + +Explanation: + +This word contains a '$' character and does not have a consonant. +``` ## 结语 diff --git a/leetcode/3101-3200/3136.Valid-Word/Solution.go b/leetcode/3101-3200/3136.Valid-Word/Solution.go index d115ccf5e..83f2ae3a9 100644 --- a/leetcode/3101-3200/3136.Valid-Word/Solution.go +++ b/leetcode/3101-3200/3136.Valid-Word/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +func isVowel(b byte) bool { + return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' || + b == 'A' || b == 'E' || b == 'I' || b == 'O' || b == 'U' +} + +func Solution(word string) bool { + if len(word) < 3 { + return false + } + a, c := false, false + for _, b := range []byte(word) { + if b >= '0' && b <= '9' { + continue + } + if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') { + if isVowel(b) { + a = true + } else { + c = true + } + continue + } + return false + } + return a && c } diff --git a/leetcode/3101-3200/3136.Valid-Word/Solution_test.go b/leetcode/3101-3200/3136.Valid-Word/Solution_test.go index 14ff50eb4..207a6f713 100644 --- a/leetcode/3101-3200/3136.Valid-Word/Solution_test.go +++ b/leetcode/3101-3200/3136.Valid-Word/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "234Adas", true}, + {"TestCase2", "b3", false}, + {"TestCase3", "a3$e", false}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }