Skip to content

Commit 524b8e7

Browse files
authored
Merge pull request #1260 from 0xff-dev/3136
Add solution and test-cases for problem 3136
2 parents 1ab2948 + 91950bf commit 524b8e7

File tree

3 files changed

+73
-20
lines changed

3 files changed

+73
-20
lines changed

leetcode/3101-3200/3136.Valid-Word/README.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
# [3136.Valid Word][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+
A word is considered **valid** if:
5+
6+
- It contains a **minimum** of 3 characters.
7+
- It contains only digits (0-9), and English letters (uppercase and lowercase).
8+
- It includes **at least** one **vowel**.
9+
- It includes **at least** one **consonant**.
10+
11+
You are given a string `word`.
12+
13+
Return `true` if `word` is valid, otherwise, return `false`.
14+
15+
**Notes**:
16+
17+
- `'a'`, `'e'`, `'i'`, `'o'`, `'u'`, and their uppercases are **vowels**.
18+
- A **consonant** is an English letter that is not a vowel.
19+
720

821
**Example 1:**
922

1023
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
24+
Input: word = "234Adas"
25+
26+
Output: true
27+
28+
Explanation:
29+
30+
This word satisfies the conditions.
1331
```
1432

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

18-
## 题解
35+
```
36+
Input: word = "b3"
37+
38+
Output: false
39+
40+
Explanation:
1941
20-
### 思路1
21-
> ...
22-
Valid Word
23-
```go
42+
The length of this word is fewer than 3, and does not have a vowel.
2443
```
2544

45+
**Example 3:**
46+
47+
```
48+
Input: word = "a3$e"
49+
50+
Output: false
51+
52+
Explanation:
53+
54+
This word contains a '$' character and does not have a consonant.
55+
```
2656

2757
## 结语
2858

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

3-
func Solution(x bool) bool {
4-
return x
3+
func isVowel(b byte) bool {
4+
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u' ||
5+
b == 'A' || b == 'E' || b == 'I' || b == 'O' || b == 'U'
6+
}
7+
8+
func Solution(word string) bool {
9+
if len(word) < 3 {
10+
return false
11+
}
12+
a, c := false, false
13+
for _, b := range []byte(word) {
14+
if b >= '0' && b <= '9' {
15+
continue
16+
}
17+
if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') {
18+
if isVowel(b) {
19+
a = true
20+
} else {
21+
c = true
22+
}
23+
continue
24+
}
25+
return false
26+
}
27+
return a && c
528
}

leetcode/3101-3200/3136.Valid-Word/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "234Adas", true},
17+
{"TestCase2", "b3", false},
18+
{"TestCase3", "a3$e", false},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)