Skip to content

Commit 7026ac4

Browse files
authored
Update README_EN.md
Added an optimized O(n) time complexity and O(1) Space Complexity.
1 parent fd17349 commit 7026ac4

File tree

1 file changed

+32
-0
lines changed
  • solution/1200-1299/1220.Count Vowels Permutation

1 file changed

+32
-0
lines changed

solution/1200-1299/1220.Count Vowels Permutation/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,39 @@ class Solution {
214214
}
215215
}
216216
```
217+
```Java(Optimized with O(n) Time Complexity and O(1) Space)
218+
class Solution {
219+
private final int mod = (int) 1e9 + 7;
220+
221+
public int countVowelPermutation(int n) {
222+
final int MOD = 1000000007;
223+
224+
long countA = 1, countE = 1, countI = 1, countO = 1, countU = 1;
225+
226+
for (int length = 1; length < n; length++) {
227+
// Calculate the next counts for each vowel based on the previous counts
228+
long nextCountA = countE;
229+
long nextCountE = (countA + countI) % MOD;
230+
long nextCountI = (countA + countE + countO + countU) % MOD;
231+
long nextCountO = (countI + countU) % MOD;
232+
long nextCountU = countA;
233+
234+
// Update the counts with the newly calculated values for the next length
235+
countA = nextCountA;
236+
countE = nextCountE;
237+
countI = nextCountI;
238+
countO = nextCountO;
239+
countU = nextCountU;
240+
}
241+
242+
// Calculate the total count of valid strings for length n
243+
long totalCount = (countA + countE + countI + countO + countU) % MOD;
244+
245+
return (int) totalCount;
246+
}
217247
248+
}
249+
```
218250
### **C++**
219251

220252
```cpp

0 commit comments

Comments
 (0)