Skip to content

Commit 1d7aaa9

Browse files
authored
feat: add solutions to lc problem: No.1849 (#3587)
No.1849.Splitting a String Into Descending Consecutive Values
1 parent 444c465 commit 1d7aaa9

File tree

7 files changed

+189
-104
lines changed

7 files changed

+189
-104
lines changed

Diff for: solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README.md

+66-34
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ tags:
8484

8585
### 方法一:DFS
8686

87-
从字符串的第一个字符开始,枚举所有可能的拆分位置,判断拆分出来的子串是否满足题目要求,如果满足则继续递归判断剩余的子串是否满足题目要求,直到遍历完整个字符串
87+
我们可以从字符串的第一个字符开始,尝试将其拆分成一个或多个子字符串,然后递归处理剩余的部分
8888

89-
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
89+
具体地,我们设计一个函数 $\textit{dfs}(i, x)$,其中 $i$ 表示当前处理到的位置,而 $x$ 表示上一个拆分出的数值。初始时 $x = -1$,表示我们还没有拆分出任何数值。
90+
91+
在 $\textit{dfs}(i, x)$ 中,我们首先计算当前拆分出的数值 $y$,如果 $x = -1$,或者 $x - y = 1$,那么我们可以尝试将 $y$ 作为下一个数值,继续递归处理剩余的部分。如果递归的结果为 $\textit{true}$,我们就找到了一种拆分方法,返回 $\textit{true}$。
92+
93+
遍历完所有的拆分方法后,如果没有找到合适的拆分方法,我们返回 $\textit{false}$。
94+
95+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。
9096

9197
<!-- tabs:start -->
9298

@@ -95,38 +101,40 @@ tags:
95101
```python
96102
class Solution:
97103
def splitString(self, s: str) -> bool:
98-
def dfs(i, x, k):
99-
if i == len(s):
100-
return k > 1
104+
def dfs(i: int, x: int) -> bool:
105+
if i >= len(s):
106+
return True
101107
y = 0
102-
for j in range(i, len(s)):
108+
r = len(s) - 1 if x < 0 else len(s)
109+
for j in range(i, r):
103110
y = y * 10 + int(s[j])
104-
if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1):
111+
if (x < 0 or x - y == 1) and dfs(j + 1, y):
105112
return True
106113
return False
107114

108-
return dfs(0, -1, 0)
115+
return dfs(0, -1)
109116
```
110117

111118
#### Java
112119

113120
```java
114121
class Solution {
115-
private String s;
122+
private char[] s;
116123

117124
public boolean splitString(String s) {
118-
this.s = s;
119-
return dfs(0, -1, 0);
125+
this.s = s.toCharArray();
126+
return dfs(0, -1);
120127
}
121128

122-
private boolean dfs(int i, long x, int k) {
123-
if (i == s.length()) {
124-
return k > 1;
129+
private boolean dfs(int i, long x) {
130+
if (i >= s.length) {
131+
return true;
125132
}
126133
long y = 0;
127-
for (int j = i; j < s.length(); ++j) {
128-
y = y * 10 + (s.charAt(j) - '0');
129-
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
134+
int r = x < 0 ? s.length - 1 : s.length;
135+
for (int j = i; j < r; ++j) {
136+
y = y * 10 + s[j] - '0';
137+
if ((x < 0 || x - y == 1) && dfs(j + 1, y)) {
130138
return true;
131139
}
132140
}
@@ -141,23 +149,24 @@ class Solution {
141149
class Solution {
142150
public:
143151
bool splitString(string s) {
144-
function<bool(int, long long, int)> dfs = [&](int i, long long x, int k) -> bool {
145-
if (i == s.size()) {
146-
return k > 1;
152+
auto dfs = [&](auto&& dfs, int i, long long x) -> bool {
153+
if (i >= s.size()) {
154+
return true;
147155
}
148156
long long y = 0;
149-
for (int j = i; j < s.size(); ++j) {
150-
y = y * 10 + (s[j] - '0');
157+
int r = x < 0 ? s.size() - 1 : s.size();
158+
for (int j = i; j < r; ++j) {
159+
y = y * 10 + s[j] - '0';
151160
if (y > 1e10) {
152161
break;
153162
}
154-
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
163+
if ((x < 0 || x - y == 1) && dfs(dfs, j + 1, y)) {
155164
return true;
156165
}
157166
}
158167
return false;
159168
};
160-
return dfs(0, -1, 0);
169+
return dfs(dfs, 0, -1);
161170
}
162171
};
163172
```
@@ -166,24 +175,47 @@ public:
166175
167176
```go
168177
func splitString(s string) bool {
169-
var dfs func(i, x, k int) bool
170-
dfs = func(i, x, k int) bool {
171-
if i == len(s) {
172-
return k > 1
178+
var dfs func(i, x int) bool
179+
dfs = func(i, x int) bool {
180+
if i >= len(s) {
181+
return true
173182
}
174183
y := 0
175-
for j := i; j < len(s); j++ {
184+
r := len(s)
185+
if x < 0 {
186+
r--
187+
}
188+
for j := i; j < r; j++ {
176189
y = y*10 + int(s[j]-'0')
177-
if y > int(1e10) {
178-
break
179-
}
180-
if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) {
190+
if (x < 0 || x-y == 1) && dfs(j+1, y) {
181191
return true
182192
}
183193
}
184194
return false
185195
}
186-
return dfs(0, -1, 0)
196+
return dfs(0, -1)
197+
}
198+
```
199+
200+
#### TypeScript
201+
202+
```ts
203+
function splitString(s: string): boolean {
204+
const dfs = (i: number, x: number): boolean => {
205+
if (i >= s.length) {
206+
return true;
207+
}
208+
let y = 0;
209+
const r = x < 0 ? s.length - 1 : s.length;
210+
for (let j = i; j < r; ++j) {
211+
y = y * 10 + +s[j];
212+
if ((x < 0 || x - y === 1) && dfs(j + 1, y)) {
213+
return true;
214+
}
215+
}
216+
return false;
217+
};
218+
return dfs(0, -1);
187219
}
188220
```
189221

Diff for: solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README_EN.md

+67-35
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ The values are in descending order with adjacent values differing by 1.
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1: DFS (Depth-First Search)
75+
### Solution 1: DFS
7676

77-
Starting from the first character of the string, enumerate all possible split positions. Check if the split substring meets the requirements of the problem. If it does, continue to recursively check whether the remaining substring meets the requirements, until the entire string is traversed.
77+
We can start from the first character of the string and try to split it into one or more substrings, then recursively process the remaining part.
7878

79-
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.
79+
Specifically, we design a function $\textit{dfs}(i, x)$, where $i$ represents the current position being processed, and $x$ represents the last split value. Initially, $x = -1$, indicating that we have not split out any value yet.
80+
81+
In $\textit{dfs}(i, x)$, we first calculate the current split value $y$. If $x = -1$, or $x - y = 1$, then we can try to use $y$ as the next value and continue to recursively process the remaining part. If the result of the recursion is $\textit{true}$, we have found a valid split method and return $\textit{true}$.
82+
83+
After traversing all possible split methods, if no valid split method is found, we return $\textit{false}$.
84+
85+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string.
8086

8187
<!-- tabs:start -->
8288

@@ -85,38 +91,40 @@ The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ i
8591
```python
8692
class Solution:
8793
def splitString(self, s: str) -> bool:
88-
def dfs(i, x, k):
89-
if i == len(s):
90-
return k > 1
94+
def dfs(i: int, x: int) -> bool:
95+
if i >= len(s):
96+
return True
9197
y = 0
92-
for j in range(i, len(s)):
98+
r = len(s) - 1 if x < 0 else len(s)
99+
for j in range(i, r):
93100
y = y * 10 + int(s[j])
94-
if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1):
101+
if (x < 0 or x - y == 1) and dfs(j + 1, y):
95102
return True
96103
return False
97104

98-
return dfs(0, -1, 0)
105+
return dfs(0, -1)
99106
```
100107

101108
#### Java
102109

103110
```java
104111
class Solution {
105-
private String s;
112+
private char[] s;
106113

107114
public boolean splitString(String s) {
108-
this.s = s;
109-
return dfs(0, -1, 0);
115+
this.s = s.toCharArray();
116+
return dfs(0, -1);
110117
}
111118

112-
private boolean dfs(int i, long x, int k) {
113-
if (i == s.length()) {
114-
return k > 1;
119+
private boolean dfs(int i, long x) {
120+
if (i >= s.length) {
121+
return true;
115122
}
116123
long y = 0;
117-
for (int j = i; j < s.length(); ++j) {
118-
y = y * 10 + (s.charAt(j) - '0');
119-
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
124+
int r = x < 0 ? s.length - 1 : s.length;
125+
for (int j = i; j < r; ++j) {
126+
y = y * 10 + s[j] - '0';
127+
if ((x < 0 || x - y == 1) && dfs(j + 1, y)) {
120128
return true;
121129
}
122130
}
@@ -131,23 +139,24 @@ class Solution {
131139
class Solution {
132140
public:
133141
bool splitString(string s) {
134-
function<bool(int, long long, int)> dfs = [&](int i, long long x, int k) -> bool {
135-
if (i == s.size()) {
136-
return k > 1;
142+
auto dfs = [&](auto&& dfs, int i, long long x) -> bool {
143+
if (i >= s.size()) {
144+
return true;
137145
}
138146
long long y = 0;
139-
for (int j = i; j < s.size(); ++j) {
140-
y = y * 10 + (s[j] - '0');
147+
int r = x < 0 ? s.size() - 1 : s.size();
148+
for (int j = i; j < r; ++j) {
149+
y = y * 10 + s[j] - '0';
141150
if (y > 1e10) {
142151
break;
143152
}
144-
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
153+
if ((x < 0 || x - y == 1) && dfs(dfs, j + 1, y)) {
145154
return true;
146155
}
147156
}
148157
return false;
149158
};
150-
return dfs(0, -1, 0);
159+
return dfs(dfs, 0, -1);
151160
}
152161
};
153162
```
@@ -156,24 +165,47 @@ public:
156165
157166
```go
158167
func splitString(s string) bool {
159-
var dfs func(i, x, k int) bool
160-
dfs = func(i, x, k int) bool {
161-
if i == len(s) {
162-
return k > 1
168+
var dfs func(i, x int) bool
169+
dfs = func(i, x int) bool {
170+
if i >= len(s) {
171+
return true
163172
}
164173
y := 0
165-
for j := i; j < len(s); j++ {
174+
r := len(s)
175+
if x < 0 {
176+
r--
177+
}
178+
for j := i; j < r; j++ {
166179
y = y*10 + int(s[j]-'0')
167-
if y > int(1e10) {
168-
break
169-
}
170-
if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) {
180+
if (x < 0 || x-y == 1) && dfs(j+1, y) {
171181
return true
172182
}
173183
}
174184
return false
175185
}
176-
return dfs(0, -1, 0)
186+
return dfs(0, -1)
187+
}
188+
```
189+
190+
#### TypeScript
191+
192+
```ts
193+
function splitString(s: string): boolean {
194+
const dfs = (i: number, x: number): boolean => {
195+
if (i >= s.length) {
196+
return true;
197+
}
198+
let y = 0;
199+
const r = x < 0 ? s.length - 1 : s.length;
200+
for (let j = i; j < r; ++j) {
201+
y = y * 10 + +s[j];
202+
if ((x < 0 || x - y === 1) && dfs(j + 1, y)) {
203+
return true;
204+
}
205+
}
206+
return false;
207+
};
208+
return dfs(0, -1);
177209
}
178210
```
179211

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
class Solution {
22
public:
33
bool splitString(string s) {
4-
function<bool(int, long long, int)> dfs = [&](int i, long long x, int k) -> bool {
5-
if (i == s.size()) {
6-
return k > 1;
4+
auto dfs = [&](auto&& dfs, int i, long long x) -> bool {
5+
if (i >= s.size()) {
6+
return true;
77
}
88
long long y = 0;
9-
for (int j = i; j < s.size(); ++j) {
10-
y = y * 10 + (s[j] - '0');
9+
int r = x < 0 ? s.size() - 1 : s.size();
10+
for (int j = i; j < r; ++j) {
11+
y = y * 10 + s[j] - '0';
1112
if (y > 1e10) {
1213
break;
1314
}
14-
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
15+
if ((x < 0 || x - y == 1) && dfs(dfs, j + 1, y)) {
1516
return true;
1617
}
1718
}
1819
return false;
1920
};
20-
return dfs(0, -1, 0);
21+
return dfs(dfs, 0, -1);
2122
}
22-
};
23+
};
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
func splitString(s string) bool {
2-
var dfs func(i, x, k int) bool
3-
dfs = func(i, x, k int) bool {
4-
if i == len(s) {
5-
return k > 1
2+
var dfs func(i, x int) bool
3+
dfs = func(i, x int) bool {
4+
if i >= len(s) {
5+
return true
66
}
77
y := 0
8-
for j := i; j < len(s); j++ {
8+
r := len(s)
9+
if x < 0 {
10+
r--
11+
}
12+
for j := i; j < r; j++ {
913
y = y*10 + int(s[j]-'0')
10-
if y > int(1e10) {
11-
break
12-
}
13-
if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) {
14+
if (x < 0 || x-y == 1) && dfs(j+1, y) {
1415
return true
1516
}
1617
}
1718
return false
1819
}
19-
return dfs(0, -1, 0)
20-
}
20+
return dfs(0, -1)
21+
}

0 commit comments

Comments
 (0)