Skip to content

Commit

Permalink
solution(python): 1220. Count Vowels Permutation
Browse files Browse the repository at this point in the history
1220. Count Vowels Permutation
- Python
  • Loading branch information
godkingjay authored Oct 30, 2023
2 parents ad3c1d9 + 25a3844 commit 5ba694b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Hard/1220. Count Vowels Permutation/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Define a constant MOD to handle modular arithmetic to avoid integer overflow.

Initialize five variables: countA, countE, countI, countO, and countU, each representing the count of valid strings of length 1 ending with the respective vowel ('a', 'e', 'i', 'o', 'u').

Use a for loop to iterate from length = 1 to n - 1 because we already have the counts for length 1, and we want to calculate the counts for lengths 2 to n.

Inside the loop, calculate the counts for each vowel for the next length based on the counts for the current length. The transitions are based on the given rules:

countA for the next length is the count of strings ending with 'e'.
countE for the next length is the count of strings ending with 'a' or 'i'.
countI for the next length is the count of strings ending with 'a', 'e', 'o', or 'u'.
countO for the next length is the count of strings ending with 'i' or 'u'.
countU for the next length is the count of strings ending with 'a'.
Update the variables countA, countE, countI, countO, and countU with the newly calculated counts for the next length.

After the loop, the variables contain the counts of valid strings of length n ending with each vowel.

Calculate the total count of valid strings for length n by summing up the counts for all vowels and take the result modulo MOD.

Return the total count as an integer.

The approach uses dynamic programming to efficiently compute the counts for different string lengths, following the rules for vowel transitions, and calculates the final result for length n.
31 changes: 31 additions & 0 deletions Hard/1220. Count Vowels Permutation/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def countVowelPermutation(self, n: int) -> int:
MOD = 1000000007

# Initialize counts for each vowel for length 1
countA = 1
countE = 1
countI = 1
countO = 1
countU = 1

# Iterate from length 1 to n - 1
for length in range(1, n):
# Calculate the next counts for each vowel based on the previous counts
nextCountA = countE
nextCountE = (countA + countI) % MOD
nextCountI = (countA + countE + countO + countU) % MOD
nextCountO = (countI + countU) % MOD
nextCountU = countA

# Update the counts with the newly calculated values for the next length
countA = nextCountA
countE = nextCountE
countI = nextCountI
countO = nextCountO
countU = nextCountU

# Calculate the total count of valid strings for length n
totalCount = (countA + countE + countI + countO + countU) % MOD

return int(totalCount)

0 comments on commit 5ba694b

Please sign in to comment.