From 7026ac4fc7b8cbdf3520a641a6acbee3b352b8f8 Mon Sep 17 00:00:00 2001 From: Ian Carson <38280903+iancarson@users.noreply.github.com> Date: Sun, 29 Oct 2023 15:06:37 -0400 Subject: [PATCH 1/4] Update README_EN.md Added an optimized O(n) time complexity and O(1) Space Complexity. --- .../README_EN.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md b/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md index 6e25c190a80b7..ee81f8dc9da76 100644 --- a/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md +++ b/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md @@ -214,7 +214,39 @@ class Solution { } } ``` +```Java(Optimized with O(n) Time Complexity and O(1) Space) +class Solution { + private final int mod = (int) 1e9 + 7; + + public int countVowelPermutation(int n) { + final int MOD = 1000000007; + + long countA = 1, countE = 1, countI = 1, countO = 1, countU = 1; + + for (int length = 1; length < n; length++) { + // Calculate the next counts for each vowel based on the previous counts + long nextCountA = countE; + long nextCountE = (countA + countI) % MOD; + long nextCountI = (countA + countE + countO + countU) % MOD; + long nextCountO = (countI + countU) % MOD; + long 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 + long totalCount = (countA + countE + countI + countO + countU) % MOD; + + return (int) totalCount; + } +} +``` ### **C++** ```cpp From bd61c0d2f0f1bd6cfbf23afc50f3b0365ebe88ce Mon Sep 17 00:00:00 2001 From: iancarson Date: Sun, 29 Oct 2023 19:10:22 +0000 Subject: [PATCH 2/4] style: format code and docs with prettier --- .../1220.Count Vowels Permutation/README_EN.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md b/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md index ee81f8dc9da76..5fbc9e0cdce0d 100644 --- a/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md +++ b/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md @@ -214,15 +214,16 @@ class Solution { } } ``` + ```Java(Optimized with O(n) Time Complexity and O(1) Space) class Solution { private final int mod = (int) 1e9 + 7; public int countVowelPermutation(int n) { final int MOD = 1000000007; - + long countA = 1, countE = 1, countI = 1, countO = 1, countU = 1; - + for (int length = 1; length < n; length++) { // Calculate the next counts for each vowel based on the previous counts long nextCountA = countE; @@ -230,7 +231,7 @@ class Solution { long nextCountI = (countA + countE + countO + countU) % MOD; long nextCountO = (countI + countU) % MOD; long nextCountU = countA; - + // Update the counts with the newly calculated values for the next length countA = nextCountA; countE = nextCountE; @@ -238,15 +239,16 @@ class Solution { countO = nextCountO; countU = nextCountU; } - + // Calculate the total count of valid strings for length n long totalCount = (countA + countE + countI + countO + countU) % MOD; - + return (int) totalCount; } } ``` + ### **C++** ```cpp From 570cedc533aba19b521bf58a1f9adb3cd6770cfc Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 30 Oct 2023 08:55:55 +0800 Subject: [PATCH 3/4] Update README_EN.md --- .../README_EN.md | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md b/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md index 5fbc9e0cdce0d..16c94e76c10a6 100644 --- a/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md +++ b/solution/1200-1299/1220.Count Vowels Permutation/README_EN.md @@ -215,23 +215,18 @@ class Solution { } ``` -```Java(Optimized with O(n) Time Complexity and O(1) Space) +```java class Solution { - private final int mod = (int) 1e9 + 7; - public int countVowelPermutation(int n) { - final int MOD = 1000000007; - + final int mod = 1000000007; long countA = 1, countE = 1, countI = 1, countO = 1, countU = 1; - for (int length = 1; length < n; length++) { // Calculate the next counts for each vowel based on the previous counts long nextCountA = countE; - long nextCountE = (countA + countI) % MOD; - long nextCountI = (countA + countE + countO + countU) % MOD; - long nextCountO = (countI + countU) % MOD; + long nextCountE = (countA + countI) % mod; + long nextCountI = (countA + countE + countO + countU) % mod; + long nextCountO = (countI + countU) % mod; long nextCountU = countA; - // Update the counts with the newly calculated values for the next length countA = nextCountA; countE = nextCountE; @@ -239,13 +234,10 @@ class Solution { countO = nextCountO; countU = nextCountU; } - // Calculate the total count of valid strings for length n - long totalCount = (countA + countE + countI + countO + countU) % MOD; - + long totalCount = (countA + countE + countI + countO + countU) % mod; return (int) totalCount; } - } ``` From fb77045623f0276a15d78daa1d2755208efeb91c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 30 Oct 2023 08:56:35 +0800 Subject: [PATCH 4/4] Update README.md --- .../1220.Count Vowels Permutation/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/solution/1200-1299/1220.Count Vowels Permutation/README.md b/solution/1200-1299/1220.Count Vowels Permutation/README.md index 0180a1370eb7d..d088f05ecb2c1 100644 --- a/solution/1200-1299/1220.Count Vowels Permutation/README.md +++ b/solution/1200-1299/1220.Count Vowels Permutation/README.md @@ -249,6 +249,32 @@ class Solution { } ``` +```java +class Solution { + public int countVowelPermutation(int n) { + final int mod = 1000000007; + long countA = 1, countE = 1, countI = 1, countO = 1, countU = 1; + for (int length = 1; length < n; length++) { + // Calculate the next counts for each vowel based on the previous counts + long nextCountA = countE; + long nextCountE = (countA + countI) % mod; + long nextCountI = (countA + countE + countO + countU) % mod; + long nextCountO = (countI + countU) % mod; + long 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 + long totalCount = (countA + countE + countI + countO + countU) % mod; + return (int) totalCount; + } +} +``` + ### **C++** ```cpp