Skip to content

Commit 600214d

Browse files
committed
Add solution #3541
1 parent 18d24dd commit 600214d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,6 +2765,7 @@
27652765
3516|[Find Closest Person](./solutions/3516-find-closest-person.js)|Easy|
27662766
3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium|
27672767
3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium|
2768+
3541|[Find Most Frequent Vowel and Consonant](./solutions/3541-find-most-frequent-vowel-and-consonant.js)|Easy|
27682769

27692770
## License
27702771

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 3541. Find Most Frequent Vowel and Consonant
3+
* https://leetcode.com/problems/find-most-frequent-vowel-and-consonant/
4+
* Difficulty: Easy
5+
*
6+
* You are given a string s consisting of lowercase English letters ('a' to 'z').
7+
*
8+
* Your task is to:
9+
* - Find the vowel (one of 'a', 'e', 'i', 'o', or 'u') with the maximum frequency.
10+
* - Find the consonant (all other letters excluding vowels) with the maximum frequency.
11+
*
12+
* Return the sum of the two frequencies.
13+
*
14+
* Note: If multiple vowels or consonants have the same maximum frequency, you may choose
15+
* any one of them. If there are no vowels or no consonants in the string, consider their
16+
* frequency as 0.
17+
*
18+
* The frequency of a letter x is the number of times it occurs in the string.
19+
*/
20+
21+
/**
22+
* @param {string} s
23+
* @return {number}
24+
*/
25+
var maxFreqSum = function(s) {
26+
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
27+
const vowelFreq = {};
28+
const consonantFreq = {};
29+
30+
for (const char of s) {
31+
if (vowels.has(char)) {
32+
vowelFreq[char] = (vowelFreq[char] || 0) + 1;
33+
} else {
34+
consonantFreq[char] = (consonantFreq[char] || 0) + 1;
35+
}
36+
}
37+
38+
const maxVowelFreq = Math.max(0, ...Object.values(vowelFreq));
39+
const maxConsonantFreq = Math.max(0, ...Object.values(consonantFreq));
40+
41+
return maxVowelFreq + maxConsonantFreq;
42+
};

0 commit comments

Comments
 (0)