Advertisement

Count Vowels Permutation - LeetCode 1220 Solution

Count Vowels Permutation - Complete Solution Guide

Count Vowels Permutation is LeetCode problem 1220, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer n , your task is to count how many strings of length n can be formed under the following rules: Each character is a lower case vowel ( 'a' , 'e' , 'i' , 'o' , 'u' ) Each vowel 'a' may only be followed by an 'e' . Each vowel 'e' may only be followed by an 'a' or an 'i' . Each vowel 'i' may not be followed by another 'i' . Each vowel 'o' may only be followed by an 'i' or a 'u' . Each vowel 'u' may only be followed by an 'a' . Since the answer may be too large, return it modulo 10^

Detailed Explanation

The problem asks us to count the number of strings of length 'n' that can be formed using lowercase vowels ('a', 'e', 'i', 'o', 'u') under specific rules regarding which vowels can follow each other. For instance, 'a' can only be followed by 'e', 'e' can only be followed by 'a' or 'i', and so on. The output should be the count of valid strings modulo 10^9 + 7 to prevent integer overflow. The input is a single integer 'n' representing the length of the string.

Solution Approach

The provided solutions utilize dynamic programming to efficiently solve this problem. The core idea is to maintain counts of how many valid strings of a given length end with each of the five vowels. We initialize these counts to 1 for strings of length 1. Then, we iterate from length 2 up to 'n', updating the counts based on the allowed transitions between vowels. For example, the number of strings of length 'i' ending in 'a' is the sum of the number of strings of length 'i-1' ending in 'e', 'i', or 'u' (since only 'e', 'i', or 'u' can precede 'a'). After iterating through all lengths, the final answer is the sum of the counts for strings of length 'n' ending in each vowel, modulo 10^9 + 7.

Step-by-Step Algorithm

  1. Step 1: Initialize five variables (a_count, e_count, i_count, o_count, u_count) to 1. These represent the number of strings of length 1 ending in each vowel.
  2. Step 2: Iterate from 2 to 'n' (inclusive).
  3. Step 3: Inside the loop, calculate the next counts (a_count_next, e_count_next, i_count_next, o_count_next, u_count_next) based on the rules. For example, a_count_next = (e_count + i_count + u_count) % MOD. The '% MOD' operation prevents integer overflow.
  4. Step 4: Update the current counts (a_count, e_count, i_count, o_count, u_count) with the next counts calculated in the previous step.
  5. Step 5: After the loop finishes, calculate the total number of strings by summing the counts of strings ending in each vowel: total = (a_count + e_count + i_count + o_count + u_count) % MOD.
  6. Step 6: Return the 'total'.

Key Insights

  • Insight 1: Dynamic programming is the most efficient approach due to the overlapping subproblems. We can build the solution iteratively from smaller string lengths.
  • Insight 2: Instead of trying to generate the strings, we can focus on counting the number of valid strings ending with each vowel for a given length.
  • Insight 3: The constraints defining valid vowel sequences are crucial and directly translate into the transition rules in the dynamic programming approach.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Dynamic Programming.

Companies

Asked at: Atlassian.