Skip to content

Commit a463f07

Browse files
authored
Merge pull request #1312 from 0xff-dev/3541
Add solution and test-cases for problem 3541
2 parents 6dbb4f3 + 5a51474 commit a463f07

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

leetcode/3501-3600/3541.Find-Most-Frequent-Vowel-and-Consonant/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [3541.Find Most Frequent Vowel and Consonant][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+
You are given a string `s` consisting of lowercase English letters (`'a'` to `'z'`).
5+
6+
Your task is to:
7+
8+
- Find the vowel (one of `'a'`, `'e'`, `'i'`, `'o'`, or `'u'`) with the **maximum** frequency.
9+
- Find the consonant (all other letters excluding vowels) with the **maximum** frequency.
10+
11+
Return the sum of the two frequencies.
12+
13+
**Note**: If multiple vowels or consonants have the same maximum frequency, you may choose any one of them. If there are no vowels or no consonants in the string, consider their frequency as 0.
14+
15+
The **frequency** of a letter `x` is the number of times it occurs in the string.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
20+
Input: s = "successes"
1421
15-
## 题意
16-
> ...
22+
Output: 6
1723
18-
## 题解
24+
Explanation:
1925
20-
### 思路1
21-
> ...
22-
Find Most Frequent Vowel and Consonant
23-
```go
26+
The vowels are: 'u' (frequency 1), 'e' (frequency 2). The maximum frequency is 2.
27+
The consonants are: 's' (frequency 4), 'c' (frequency 2). The maximum frequency is 4.
28+
The output is 2 + 4 = 6.
2429
```
2530

31+
**Example 2:**
32+
33+
```
34+
Input: s = "aeiaeia"
35+
36+
Output: 3
37+
38+
Explanation:
39+
40+
The vowels are: 'a' (frequency 3), 'e' ( frequency 2), 'i' (frequency 2). The maximum frequency is 3.
41+
There are no consonants in s. Hence, maximum consonant frequency = 0.
42+
The output is 3 + 0 = 3.
43+
```
2644

2745
## 结语
2846

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
count := [26]int{}
5+
for _, b := range []byte(s) {
6+
count[b-'a']++
7+
}
8+
vowel, consonant := 0, 0
9+
for i := 'a'; i <= 'z'; i++ {
10+
if i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u' {
11+
vowel = max(vowel, count[i-'a'])
12+
continue
13+
}
14+
consonant = max(consonant, count[i-'a'])
15+
}
16+
return vowel + consonant
517
}

leetcode/3501-3600/3541.Find-Most-Frequent-Vowel-and-Consonant/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "successes", 6},
17+
{"TestCase2", "aeiaeia", 3},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)