Skip to content

Commit 6fdcc26

Browse files
authored
feat: add solutions to lc problem: No.3541 (#4400)
No.3541.Find Most Frequent Vowel and Consonant
1 parent 3f5067f commit 6fdcc26

File tree

7 files changed

+263
-8
lines changed

7 files changed

+263
-8
lines changed

solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README.md

+92-4
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,120 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3541.Fi
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:计数
72+
73+
我们先用一个哈希表或者一个长度为 $26$ 的数组 $\textit{cnt}$ 统计每个字母的出现频率。然后我们遍历这个表,找出元音和辅音中出现频率最高的字母,返回它们的频率之和。
74+
75+
我们可以用一个变量 $\textit{a}$ 记录元音的最大频率,另一个变量 $\textit{b}$ 记录辅音的最大频率。遍历时,如果当前字母是元音,就更新 $\textit{a}$;否则就更新 $\textit{b}$。
76+
77+
最后返回 $\textit{a} + \textit{b}$ 即可。
78+
79+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $(|\Sigma|)$,其中 $|\Sigma|$ 是字母表的大小,这里是 $26$。
7280

7381
<!-- tabs:start -->
7482

7583
#### Python3
7684

7785
```python
78-
86+
class Solution:
87+
def maxFreqSum(self, s: str) -> int:
88+
cnt = Counter(s)
89+
a = b = 0
90+
for c, v in cnt.items():
91+
if c in "aeiou":
92+
a = max(a, v)
93+
else:
94+
b = max(b, v)
95+
return a + b
7996
```
8097

8198
#### Java
8299

83100
```java
84-
101+
class Solution {
102+
public int maxFreqSum(String s) {
103+
int[] cnt = new int[26];
104+
for (char c : s.toCharArray()) {
105+
++cnt[c - 'a'];
106+
}
107+
int a = 0, b = 0;
108+
for (int i = 0; i < cnt.length; ++i) {
109+
char c = (char) (i + 'a');
110+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
111+
a = Math.max(a, cnt[i]);
112+
} else {
113+
b = Math.max(b, cnt[i]);
114+
}
115+
}
116+
return a + b;
117+
}
118+
}
85119
```
86120

87121
#### C++
88122

89123
```cpp
90-
124+
class Solution {
125+
public:
126+
int maxFreqSum(string s) {
127+
int cnt[26]{};
128+
for (char c : s) {
129+
++cnt[c - 'a'];
130+
}
131+
int a = 0, b = 0;
132+
for (int i = 0; i < 26; ++i) {
133+
char c = 'a' + i;
134+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
135+
a = max(a, cnt[i]);
136+
} else {
137+
b = max(b, cnt[i]);
138+
}
139+
}
140+
return a + b;
141+
}
142+
};
91143
```
92144
93145
#### Go
94146
95147
```go
148+
func maxFreqSum(s string) int {
149+
cnt := [26]int{}
150+
for _, c := range s {
151+
cnt[c-'a']++
152+
}
153+
a, b := 0, 0
154+
for i := range cnt {
155+
c := byte(i + 'a')
156+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
157+
a = max(a, cnt[i])
158+
} else {
159+
b = max(b, cnt[i])
160+
}
161+
}
162+
return a + b
163+
}
164+
```
96165

166+
#### TypeScript
167+
168+
```ts
169+
function maxFreqSum(s: string): number {
170+
const cnt: number[] = Array(26).fill(0);
171+
for (const c of s) {
172+
++cnt[c.charCodeAt(0) - 97];
173+
}
174+
let [a, b] = [0, 0];
175+
for (let i = 0; i < 26; ++i) {
176+
const c = String.fromCharCode(i + 97);
177+
if ('aeiou'.includes(c)) {
178+
a = Math.max(a, cnt[i]);
179+
} else {
180+
b = Math.max(b, cnt[i]);
181+
}
182+
}
183+
return a + b;
184+
}
97185
```
98186

99187
<!-- tabs:end -->

solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README_EN.md

+92-4
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,120 @@ The <strong>frequency</strong> of a letter <code>x</code> is the number of times
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1
77+
### Solution 1: Counting
78+
79+
We first use a hash table or an array of length $26$, $\textit{cnt}$, to count the frequency of each letter. Then, we iterate through this table to find the most frequent vowel and consonant, and return the sum of their frequencies.
80+
81+
We can use a variable $\textit{a}$ to record the maximum frequency of vowels and another variable $\textit{b}$ to record the maximum frequency of consonants. During the iteration, if the current letter is a vowel, we update $\textit{a}$; otherwise, we update $\textit{b}$.
82+
83+
Finally, we return $\textit{a} + \textit{b}$.
84+
85+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(|\Sigma|)$, where $|\Sigma|$ is the size of the alphabet, which is $26$ in this case.
7886

7987
<!-- tabs:start -->
8088

8189
#### Python3
8290

8391
```python
84-
92+
class Solution:
93+
def maxFreqSum(self, s: str) -> int:
94+
cnt = Counter(s)
95+
a = b = 0
96+
for c, v in cnt.items():
97+
if c in "aeiou":
98+
a = max(a, v)
99+
else:
100+
b = max(b, v)
101+
return a + b
85102
```
86103

87104
#### Java
88105

89106
```java
90-
107+
class Solution {
108+
public int maxFreqSum(String s) {
109+
int[] cnt = new int[26];
110+
for (char c : s.toCharArray()) {
111+
++cnt[c - 'a'];
112+
}
113+
int a = 0, b = 0;
114+
for (int i = 0; i < cnt.length; ++i) {
115+
char c = (char) (i + 'a');
116+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
117+
a = Math.max(a, cnt[i]);
118+
} else {
119+
b = Math.max(b, cnt[i]);
120+
}
121+
}
122+
return a + b;
123+
}
124+
}
91125
```
92126

93127
#### C++
94128

95129
```cpp
96-
130+
class Solution {
131+
public:
132+
int maxFreqSum(string s) {
133+
int cnt[26]{};
134+
for (char c : s) {
135+
++cnt[c - 'a'];
136+
}
137+
int a = 0, b = 0;
138+
for (int i = 0; i < 26; ++i) {
139+
char c = 'a' + i;
140+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
141+
a = max(a, cnt[i]);
142+
} else {
143+
b = max(b, cnt[i]);
144+
}
145+
}
146+
return a + b;
147+
}
148+
};
97149
```
98150
99151
#### Go
100152
101153
```go
154+
func maxFreqSum(s string) int {
155+
cnt := [26]int{}
156+
for _, c := range s {
157+
cnt[c-'a']++
158+
}
159+
a, b := 0, 0
160+
for i := range cnt {
161+
c := byte(i + 'a')
162+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
163+
a = max(a, cnt[i])
164+
} else {
165+
b = max(b, cnt[i])
166+
}
167+
}
168+
return a + b
169+
}
170+
```
102171

172+
#### TypeScript
173+
174+
```ts
175+
function maxFreqSum(s: string): number {
176+
const cnt: number[] = Array(26).fill(0);
177+
for (const c of s) {
178+
++cnt[c.charCodeAt(0) - 97];
179+
}
180+
let [a, b] = [0, 0];
181+
for (let i = 0; i < 26; ++i) {
182+
const c = String.fromCharCode(i + 97);
183+
if ('aeiou'.includes(c)) {
184+
a = Math.max(a, cnt[i]);
185+
} else {
186+
b = Math.max(b, cnt[i]);
187+
}
188+
}
189+
return a + b;
190+
}
103191
```
104192

105193
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxFreqSum(string s) {
4+
int cnt[26]{};
5+
for (char c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
int a = 0, b = 0;
9+
for (int i = 0; i < 26; ++i) {
10+
char c = 'a' + i;
11+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
12+
a = max(a, cnt[i]);
13+
} else {
14+
b = max(b, cnt[i]);
15+
}
16+
}
17+
return a + b;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func maxFreqSum(s string) int {
2+
cnt := [26]int{}
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
a, b := 0, 0
7+
for i := range cnt {
8+
c := byte(i + 'a')
9+
if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
10+
a = max(a, cnt[i])
11+
} else {
12+
b = max(b, cnt[i])
13+
}
14+
}
15+
return a + b
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxFreqSum(String s) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
int a = 0, b = 0;
8+
for (int i = 0; i < cnt.length; ++i) {
9+
char c = (char) (i + 'a');
10+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
11+
a = Math.max(a, cnt[i]);
12+
} else {
13+
b = Math.max(b, cnt[i]);
14+
}
15+
}
16+
return a + b;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxFreqSum(self, s: str) -> int:
3+
cnt = Counter(s)
4+
a = b = 0
5+
for c, v in cnt.items():
6+
if c in "aeiou":
7+
a = max(a, v)
8+
else:
9+
b = max(b, v)
10+
return a + b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxFreqSum(s: string): number {
2+
const cnt: number[] = Array(26).fill(0);
3+
for (const c of s) {
4+
++cnt[c.charCodeAt(0) - 97];
5+
}
6+
let [a, b] = [0, 0];
7+
for (let i = 0; i < 26; ++i) {
8+
const c = String.fromCharCode(i + 97);
9+
if ('aeiou'.includes(c)) {
10+
a = Math.max(a, cnt[i]);
11+
} else {
12+
b = Math.max(b, cnt[i]);
13+
}
14+
}
15+
return a + b;
16+
}

0 commit comments

Comments
 (0)