Skip to content

Commit bd4f219

Browse files
committed
feat: add solutions to lc problems: No.1220,1221
* No.1220.Count Vowels Permutation * No.1221.Split a String in Balanced Strings
1 parent b03dca0 commit bd4f219

File tree

13 files changed

+226
-76
lines changed

13 files changed

+226
-76
lines changed

Diff for: solution/1200-1299/1220.Count Vowels Permutation/README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:动态规划**
56+
57+
根据题目描述,我们可以推出每个元音字母的前一个字母可以为哪些。
58+
5559
```bash
5660
a [e]
5761
e [a|i]
@@ -68,7 +72,7 @@ u [a]
6872
[i|o] u
6973
```
7074

71-
`dp[i][j]` 表示当前长度为 i 且以字符 j 为结尾的字符串的数目,其中 j = {0,1,2,3,4} 分别代表元音字母 `a,e,i,o,u`
75+
$dp[i][j]$ 表示当前长度为 $i$ 且以字符 j 为结尾的字符串的数目,其中 j = {0,1,2,3,4} 分别代表元音字母 `a,e,i,o,u`
7276

7377
<!-- tabs:start -->
7478

@@ -162,6 +166,33 @@ func countVowelPermutation(n int) int {
162166
}
163167
```
164168

169+
### **JavaScript**
170+
171+
```js
172+
/**
173+
* @param {number} n
174+
* @return {number}
175+
*/
176+
var countVowelPermutation = function (n) {
177+
const mod = 1000000007;
178+
const dp = new Array(5).fill(1);
179+
const t = new Array(5).fill(0);
180+
for (let i = 0; i < n - 1; ++i) {
181+
t[0] = (dp[1] + dp[2] + dp[4]) % mod;
182+
t[1] = (dp[0] + dp[2]) % mod;
183+
t[2] = (dp[1] + dp[3]) % mod;
184+
t[3] = dp[2];
185+
t[4] = (dp[2] + dp[3]) % mod;
186+
dp.splice(0, 5, ...t);
187+
}
188+
let ans = 0;
189+
for (let i = 0; i < 5; ++i) {
190+
ans = (ans + dp[i]) % mod;
191+
}
192+
return ans;
193+
};
194+
```
195+
165196
### **...**
166197

167198
```

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

+27
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,33 @@ func countVowelPermutation(n int) int {
153153
}
154154
```
155155

156+
### **JavaScript**
157+
158+
```js
159+
/**
160+
* @param {number} n
161+
* @return {number}
162+
*/
163+
var countVowelPermutation = function (n) {
164+
const mod = 1000000007;
165+
const dp = new Array(5).fill(1);
166+
const t = new Array(5).fill(0);
167+
for (let i = 0; i < n - 1; ++i) {
168+
t[0] = (dp[1] + dp[2] + dp[4]) % mod;
169+
t[1] = (dp[0] + dp[2]) % mod;
170+
t[2] = (dp[1] + dp[3]) % mod;
171+
t[3] = dp[2];
172+
t[4] = (dp[2] + dp[3]) % mod;
173+
dp.splice(0, 5, ...t);
174+
}
175+
let ans = 0;
176+
for (let i = 0; i < 5; ++i) {
177+
ans = (ans + dp[i]) % mod;
178+
}
179+
return ans;
180+
};
181+
```
182+
156183
### **...**
157184

158185
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var countVowelPermutation = function (n) {
6+
const mod = 1000000007;
7+
const dp = new Array(5).fill(1);
8+
const t = new Array(5).fill(0);
9+
for (let i = 0; i < n - 1; ++i) {
10+
t[0] = (dp[1] + dp[2] + dp[4]) % mod;
11+
t[1] = (dp[0] + dp[2]) % mod;
12+
t[2] = (dp[1] + dp[3]) % mod;
13+
t[3] = dp[2];
14+
t[4] = (dp[2] + dp[3]) % mod;
15+
dp.splice(0, 5, ...t);
16+
}
17+
let ans = 0;
18+
for (let i = 0; i < 5; ++i) {
19+
ans = (ans + dp[i]) % mod;
20+
}
21+
return ans;
22+
};

Diff for: solution/1200-1299/1221.Split a String in Balanced Strings/README.md

+49-24
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@
7171
```python
7272
class Solution:
7373
def balancedStringSplit(self, s: str) -> int:
74-
n = res = 0
74+
ans = l = 0
7575
for c in s:
7676
if c == 'L':
77-
n += 1
77+
l += 1
7878
else:
79-
n -= 1
80-
if n == 0:
81-
res += 1
82-
return res
79+
l -= 1
80+
if l == 0:
81+
ans += 1
82+
return ans
8383
```
8484

8585
### **Java**
@@ -89,18 +89,18 @@ class Solution:
8989
```java
9090
class Solution {
9191
public int balancedStringSplit(String s) {
92-
int n = 0, res = 0;
92+
int ans = 0, l = 0;
9393
for (char c : s.toCharArray()) {
9494
if (c == 'L') {
95-
++n;
95+
++l;
9696
} else {
97-
--n;
97+
--l;
9898
}
99-
if (n == 0) {
100-
++res;
99+
if (l == 0) {
100+
++ans;
101101
}
102102
}
103-
return res;
103+
return ans;
104104
}
105105
}
106106
```
@@ -111,13 +111,14 @@ class Solution {
111111
class Solution {
112112
public:
113113
int balancedStringSplit(string s) {
114-
int n = 0, res = 0;
115-
for (char c : s) {
116-
if (c == 'L') ++n;
117-
else --n;
118-
if (n == 0) ++res;
114+
int ans = 0, l = 0;
115+
for (char c : s)
116+
{
117+
if (c == 'L') ++l;
118+
else --l;
119+
if (l == 0) ++ans;
119120
}
120-
return res;
121+
return ans;
121122
}
122123
};
123124
```
@@ -126,21 +127,45 @@ public:
126127
127128
```go
128129
func balancedStringSplit(s string) int {
129-
n, res := 0, 0
130+
ans, l := 0, 0
130131
for _, c := range s {
131132
if c == 'L' {
132-
n++
133+
l++
133134
} else {
134-
n--
135+
l--
135136
}
136-
if n == 0 {
137-
res++
137+
if l == 0 {
138+
ans++
138139
}
139140
}
140-
return res
141+
return ans
141142
}
142143
```
143144

145+
### **JavaScript**
146+
147+
```js
148+
/**
149+
* @param {string} s
150+
* @return {number}
151+
*/
152+
var balancedStringSplit = function (s) {
153+
let ans = 0;
154+
let l = 0;
155+
for (let c of s) {
156+
if (c == 'L') {
157+
++l;
158+
} else {
159+
--l;
160+
}
161+
if (l == 0) {
162+
++ans;
163+
}
164+
}
165+
return ans;
166+
};
167+
```
168+
144169
### **...**
145170

146171
```

Diff for: solution/1200-1299/1221.Split a String in Balanced Strings/README_EN.md

+49-24
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,34 @@
5353
```python
5454
class Solution:
5555
def balancedStringSplit(self, s: str) -> int:
56-
n = res = 0
56+
ans = l = 0
5757
for c in s:
5858
if c == 'L':
59-
n += 1
59+
l += 1
6060
else:
61-
n -= 1
62-
if n == 0:
63-
res += 1
64-
return res
61+
l -= 1
62+
if l == 0:
63+
ans += 1
64+
return ans
6565
```
6666

6767
### **Java**
6868

6969
```java
7070
class Solution {
7171
public int balancedStringSplit(String s) {
72-
int n = 0, res = 0;
72+
int ans = 0, l = 0;
7373
for (char c : s.toCharArray()) {
7474
if (c == 'L') {
75-
++n;
75+
++l;
7676
} else {
77-
--n;
77+
--l;
7878
}
79-
if (n == 0) {
80-
++res;
79+
if (l == 0) {
80+
++ans;
8181
}
8282
}
83-
return res;
83+
return ans;
8484
}
8585
}
8686
```
@@ -91,13 +91,14 @@ class Solution {
9191
class Solution {
9292
public:
9393
int balancedStringSplit(string s) {
94-
int n = 0, res = 0;
95-
for (char c : s) {
96-
if (c == 'L') ++n;
97-
else --n;
98-
if (n == 0) ++res;
94+
int ans = 0, l = 0;
95+
for (char c : s)
96+
{
97+
if (c == 'L') ++l;
98+
else --l;
99+
if (l == 0) ++ans;
99100
}
100-
return res;
101+
return ans;
101102
}
102103
};
103104
```
@@ -106,21 +107,45 @@ public:
106107
107108
```go
108109
func balancedStringSplit(s string) int {
109-
n, res := 0, 0
110+
ans, l := 0, 0
110111
for _, c := range s {
111112
if c == 'L' {
112-
n++
113+
l++
113114
} else {
114-
n--
115+
l--
115116
}
116-
if n == 0 {
117-
res++
117+
if l == 0 {
118+
ans++
118119
}
119120
}
120-
return res
121+
return ans
121122
}
122123
```
123124

125+
### **JavaScript**
126+
127+
```js
128+
/**
129+
* @param {string} s
130+
* @return {number}
131+
*/
132+
var balancedStringSplit = function (s) {
133+
let ans = 0;
134+
let l = 0;
135+
for (let c of s) {
136+
if (c == 'L') {
137+
++l;
138+
} else {
139+
--l;
140+
}
141+
if (l == 0) {
142+
++ans;
143+
}
144+
}
145+
return ans;
146+
};
147+
```
148+
124149
### **...**
125150

126151
```
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public:
33
int balancedStringSplit(string s) {
4-
int n = 0, res = 0;
5-
for (char c : s) {
6-
if (c == 'L') ++n;
7-
else --n;
8-
if (n == 0) ++res;
4+
int ans = 0, l = 0;
5+
for (char c : s)
6+
{
7+
if (c == 'L') ++l;
8+
else --l;
9+
if (l == 0) ++ans;
910
}
10-
return res;
11+
return ans;
1112
}
1213
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
func balancedStringSplit(s string) int {
2-
n, res := 0, 0
2+
ans, l := 0, 0
33
for _, c := range s {
44
if c == 'L' {
5-
n++
5+
l++
66
} else {
7-
n--
7+
l--
88
}
9-
if n == 0 {
10-
res++
9+
if l == 0 {
10+
ans++
1111
}
1212
}
13-
return res
13+
return ans
1414
}

0 commit comments

Comments
 (0)