You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README_EN.md
+92-4
Original file line number
Diff line number
Diff line change
@@ -74,32 +74,120 @@ The <strong>frequency</strong> of a letter <code>x</code> is the number of times
74
74
75
75
<!-- solution:start -->
76
76
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.
78
86
79
87
<!-- tabs:start -->
80
88
81
89
#### Python3
82
90
83
91
```python
84
-
92
+
classSolution:
93
+
defmaxFreqSum(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
85
102
```
86
103
87
104
#### Java
88
105
89
106
```java
90
-
107
+
classSolution {
108
+
publicintmaxFreqSum(Strings) {
109
+
int[] cnt =newint[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
+
}
91
125
```
92
126
93
127
#### C++
94
128
95
129
```cpp
96
-
130
+
classSolution {
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
+
};
97
149
```
98
150
99
151
#### Go
100
152
101
153
```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' {
0 commit comments