19. Count Unique Vowel Strings
β GFG solution to Count Unique Vowel Strings problem: calculate total distinct strings by selecting vowels and forming permutations using combinatorial mathematics. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a lowercase string s
, determine the total number of distinct strings that can be formed using the following rules:
Identify all unique vowels (a, e, i, o, u) present in the string.
Select exactly one occurrence of each distinct vowel from
s
. If a vowel appears multiple times, each occurrence represents a unique selection choice.Generate all possible permutations of the selected vowels. Each unique arrangement counts as a distinct string.
Return the total number of such distinct strings.
π Examples
Example 1
Input: s = "aeiou"
Output: 120
Explanation: Each vowel appears once, so the number of different strings can form is 5! = 120.
Example 2
Input: s = "ae"
Output: 2
Explanation: Pick a and e, make all orders β "ae", "ea".
Example 3
Input: s = "aacidf"
Output: 4
Explanation: Vowels in s are 'a' and 'i'. Pick each 'a' once with a single 'i',
make all orders β "ai", "ia", "ai", "ia".
Since 'a' appears twice, we have 2 ways to pick 'a' and 1 way to pick 'i'.
Total selections = 2 Γ 1 = 2. Permutations of 2 vowels = 2! = 2.
Total distinct strings = 2 Γ 2 = 4.
π Constraints
$1 \le s.size() \le 100$
β
My Approach
The solution uses Combinatorial Mathematics with Frequency Counting to calculate the result efficiently:
Frequency Counting + Combinatorics
Count Vowel Frequencies:
Iterate through the string and count occurrences of each vowel (a, e, i, o, u).
Store frequencies in an array or map.
Calculate Selection Ways:
For each vowel that appears in the string, the number of ways to select one occurrence equals its frequency.
Multiply frequencies of all present vowels to get total selection combinations.
Calculate Permutations:
Count the number of distinct vowel types present.
Calculate factorial of this count to get all possible arrangements.
Final Result:
Total distinct strings = (Product of frequencies) Γ (Factorial of distinct vowel count)
Mathematical Formula:
Result = (β frequency[vowel]) Γ (distinct_vowels)!
Where:
β frequency[vowel]
= product of frequencies of all vowels presentdistinct_vowels
= count of unique vowel types in the string
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the length of the string. We traverse the string once to count vowel frequencies, then perform constant operations for calculation.
Expected Auxiliary Space Complexity: O(1), as we use a fixed-size array to store vowel frequencies (maximum 5 vowels) and a few variables for calculation.
π§βπ» Code (C++)
class Solution {
public:
int vowelCount(string& s) {
int freq[26] = {}, cnt = 0, mul = 1;
for (char c : s) if (strchr("aeiou", c)) freq[c - 'a']++;
for (int v : {0, 4, 8, 14, 20}) if (freq[v]) mul *= freq[v], cnt++;
return cnt ? mul * tgamma(cnt + 1) : 0;
}
};
β Code (Java)
class Solution {
public int vowelCount(String s) {
int[] f = new int[5];
for (char c : s.toCharArray())
if ("aeiou".indexOf(c) >= 0) f["aeiou".indexOf(c)]++;
int cnt = 0, prod = 1;
for (int x : f) if (x > 0) { prod *= x; cnt++; }
for (int i = 2; i <= cnt; i++) prod *= i;
return cnt == 0 ? 0 : prod;
}
}
π Code (Python)
from math import prod, factorial
from collections import Counter
class Solution:
def vowelCount(self, s):
freq = Counter(c for c in s if c in 'aeiou')
vals = list(freq.values())
return prod(vals) * factorial(len(vals)) if vals else 0
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated