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:

  1. Identify all unique vowels (a, e, i, o, u) present in the string.

  2. Select exactly one occurrence of each distinct vowel from s. If a vowel appears multiple times, each occurrence represents a unique selection choice.

  3. 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

πŸ”’ 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

  1. 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.

  2. 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.

  3. Calculate Permutations:

    • Count the number of distinct vowel types present.

    • Calculate factorial of this count to get all possible arrangements.

  4. Final Result:

    • Total distinct strings = (Product of frequencies) Γ— (Factorial of distinct vowel count)

Mathematical Formula:

Where:

  • ∏ frequency[vowel] = product of frequencies of all vowels present

  • distinct_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++)

⚑ View Alternative Approaches with Code and Analysis

πŸ“Š 2️⃣ Optimized Counting with Map

πŸ’‘ Algorithm Steps:

  1. Use unordered_map for vowel frequency tracking

  2. Calculate factorial iteratively for better precision

  3. Handle edge cases efficiently

  4. Minimize memory footprint

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - single pass through string

  • Auxiliary Space: πŸ’Ύ O(1) - fixed vowel storage

βœ… Why This Approach?

  • Better precision than tgamma

  • More readable code structure

  • Handles large frequencies better

πŸ“Š 3️⃣ Switch-Case Optimization

πŸ’‘ Algorithm Steps:

  1. Use switch-case for O(1) vowel detection

  2. Avoid string operations entirely

  3. Direct array indexing

  4. Minimal branching

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) – single pass with O(1) checks per character

  • Auxiliary Space: πŸ’Ύ O(1) – fixed-size arrays only

βœ… Why This Approach?

  • Fastest character lookup via switch

  • Compile-time branch resolution

  • Branch-free vowel identification (no string searches)

πŸ“Š 4️⃣ String View Optimization

πŸ’‘ Algorithm Steps:

  1. Use string_view for faster character access

  2. Lookup table for vowel identification

  3. Single pass with minimal operations

  4. Compile-time optimizations

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - optimized with lookup tables

  • Auxiliary Space: πŸ’Ύ O(1) - constant space arrays

βœ… Why This Approach?

  • Fastest character lookup

  • Compile-time optimizations

  • Branch-free vowel detection

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

πŸ” Array + tgamma

🟒 O(n)

🟒 O(1)

πŸš€ Compact and fast

πŸ’Ύ Potential precision issues

πŸ”Ί Map-based Counting

🟒 O(n)

🟒 O(1)

πŸ”§ Better precision

πŸ’Ύ Slightly more memory

⏰ Switch-Case

🟒 O(n)

🟒 O(1)

πŸš€ O(1) character lookup

πŸ”„ More lines of code

πŸ“Š Lookup Table

🟒 O(n)

🟒 O(1)

⚑ Fastest character lookup

πŸ”§ Larger code size

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

⚑ Speed-critical applications

πŸ₯‡ Lookup Table

β˜…β˜…β˜…β˜…β˜…

πŸ“Š General purpose

πŸ₯ˆ Array + tgamma

β˜…β˜…β˜…β˜…β˜†

🎯 High precision required

πŸ₯‰ Map-based Counting

β˜…β˜…β˜…β˜…β˜†

πŸš€ Memory-constrained

πŸ… Switch-Case

β˜…β˜…β˜…β˜…β˜†

β˜• Code (Java)

🐍 Code (Python)

🧠 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

Visitor counter

Last updated