-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution(python): 1220. Count Vowels Permutation
1220. Count Vowels Permutation - Python
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |