LeetCode-in-Java

3541. Find Most Frequent Vowel and Consonant

Easy

You are given a string s consisting of lowercase English letters ('a' to 'z').

Your task is to:

Return the sum of the two frequencies.

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.

The frequency of a letter x is the number of times it occurs in the string.

Example 1:

Input: s = “successes”

Output: 6

Explanation:

Example 2:

Input: s = “aeiaeia”

Output: 3

Explanation:

Constraints:

Solution

public class Solution {
    public int maxFreqSum(String s) {
        int[] freq = new int[26];
        for (char ch : s.toCharArray()) {
            int index = ch - 'a';
            freq[index]++;
        }
        String si = "aeiou";
        int max1 = 0;
        int max2 = 0;
        for (int i = 0; i < 26; i++) {
            char ch = (char) (i + 'a');
            if (si.indexOf(ch) != -1) {
                max1 = Math.max(max1, freq[i]);
            } else {
                max2 = Math.max(max2, freq[i]);
            }
        }
        return max1 + max2;
    }
}