Skip to content

feat: add solutions to lc problem: No.1849 #3587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ tags:

### 方法一:DFS

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

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
具体地,我们设计一个函数 $\textit{dfs}(i, x)$,其中 $i$ 表示当前处理到的位置,而 $x$ 表示上一个拆分出的数值。初始时 $x = -1$,表示我们还没有拆分出任何数值。

在 $\textit{dfs}(i, x)$ 中,我们首先计算当前拆分出的数值 $y$,如果 $x = -1$,或者 $x - y = 1$,那么我们可以尝试将 $y$ 作为下一个数值,继续递归处理剩余的部分。如果递归的结果为 $\textit{true}$,我们就找到了一种拆分方法,返回 $\textit{true}$。

遍历完所有的拆分方法后,如果没有找到合适的拆分方法,我们返回 $\textit{false}$。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。

<!-- tabs:start -->

Expand All @@ -95,38 +101,40 @@ tags:
```python
class Solution:
def splitString(self, s: str) -> bool:
def dfs(i, x, k):
if i == len(s):
return k > 1
def dfs(i: int, x: int) -> bool:
if i >= len(s):
return True
y = 0
for j in range(i, len(s)):
r = len(s) - 1 if x < 0 else len(s)
for j in range(i, r):
y = y * 10 + int(s[j])
if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1):
if (x < 0 or x - y == 1) and dfs(j + 1, y):
return True
return False

return dfs(0, -1, 0)
return dfs(0, -1)
```

#### Java

```java
class Solution {
private String s;
private char[] s;

public boolean splitString(String s) {
this.s = s;
return dfs(0, -1, 0);
this.s = s.toCharArray();
return dfs(0, -1);
}

private boolean dfs(int i, long x, int k) {
if (i == s.length()) {
return k > 1;
private boolean dfs(int i, long x) {
if (i >= s.length) {
return true;
}
long y = 0;
for (int j = i; j < s.length(); ++j) {
y = y * 10 + (s.charAt(j) - '0');
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
int r = x < 0 ? s.length - 1 : s.length;
for (int j = i; j < r; ++j) {
y = y * 10 + s[j] - '0';
if ((x < 0 || x - y == 1) && dfs(j + 1, y)) {
return true;
}
}
Expand All @@ -141,23 +149,24 @@ class Solution {
class Solution {
public:
bool splitString(string s) {
function<bool(int, long long, int)> dfs = [&](int i, long long x, int k) -> bool {
if (i == s.size()) {
return k > 1;
auto dfs = [&](auto&& dfs, int i, long long x) -> bool {
if (i >= s.size()) {
return true;
}
long long y = 0;
for (int j = i; j < s.size(); ++j) {
y = y * 10 + (s[j] - '0');
int r = x < 0 ? s.size() - 1 : s.size();
for (int j = i; j < r; ++j) {
y = y * 10 + s[j] - '0';
if (y > 1e10) {
break;
}
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
if ((x < 0 || x - y == 1) && dfs(dfs, j + 1, y)) {
return true;
}
}
return false;
};
return dfs(0, -1, 0);
return dfs(dfs, 0, -1);
}
};
```
Expand All @@ -166,24 +175,47 @@ public:

```go
func splitString(s string) bool {
var dfs func(i, x, k int) bool
dfs = func(i, x, k int) bool {
if i == len(s) {
return k > 1
var dfs func(i, x int) bool
dfs = func(i, x int) bool {
if i >= len(s) {
return true
}
y := 0
for j := i; j < len(s); j++ {
r := len(s)
if x < 0 {
r--
}
for j := i; j < r; j++ {
y = y*10 + int(s[j]-'0')
if y > int(1e10) {
break
}
if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) {
if (x < 0 || x-y == 1) && dfs(j+1, y) {
return true
}
}
return false
}
return dfs(0, -1, 0)
return dfs(0, -1)
}
```

#### TypeScript

```ts
function splitString(s: string): boolean {
const dfs = (i: number, x: number): boolean => {
if (i >= s.length) {
return true;
}
let y = 0;
const r = x < 0 ? s.length - 1 : s.length;
for (let j = i; j < r; ++j) {
y = y * 10 + +s[j];
if ((x < 0 || x - y === 1) && dfs(j + 1, y)) {
return true;
}
}
return false;
};
return dfs(0, -1);
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,17 @@ The values are in descending order with adjacent values differing by 1.

<!-- solution:start -->

### Solution 1: DFS (Depth-First Search)
### Solution 1: DFS

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.
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.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.
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.

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}$.

After traversing all possible split methods, if no valid split method is found, we return $\textit{false}$.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string.

<!-- tabs:start -->

Expand All @@ -85,38 +91,40 @@ The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ i
```python
class Solution:
def splitString(self, s: str) -> bool:
def dfs(i, x, k):
if i == len(s):
return k > 1
def dfs(i: int, x: int) -> bool:
if i >= len(s):
return True
y = 0
for j in range(i, len(s)):
r = len(s) - 1 if x < 0 else len(s)
for j in range(i, r):
y = y * 10 + int(s[j])
if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1):
if (x < 0 or x - y == 1) and dfs(j + 1, y):
return True
return False

return dfs(0, -1, 0)
return dfs(0, -1)
```

#### Java

```java
class Solution {
private String s;
private char[] s;

public boolean splitString(String s) {
this.s = s;
return dfs(0, -1, 0);
this.s = s.toCharArray();
return dfs(0, -1);
}

private boolean dfs(int i, long x, int k) {
if (i == s.length()) {
return k > 1;
private boolean dfs(int i, long x) {
if (i >= s.length) {
return true;
}
long y = 0;
for (int j = i; j < s.length(); ++j) {
y = y * 10 + (s.charAt(j) - '0');
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
int r = x < 0 ? s.length - 1 : s.length;
for (int j = i; j < r; ++j) {
y = y * 10 + s[j] - '0';
if ((x < 0 || x - y == 1) && dfs(j + 1, y)) {
return true;
}
}
Expand All @@ -131,23 +139,24 @@ class Solution {
class Solution {
public:
bool splitString(string s) {
function<bool(int, long long, int)> dfs = [&](int i, long long x, int k) -> bool {
if (i == s.size()) {
return k > 1;
auto dfs = [&](auto&& dfs, int i, long long x) -> bool {
if (i >= s.size()) {
return true;
}
long long y = 0;
for (int j = i; j < s.size(); ++j) {
y = y * 10 + (s[j] - '0');
int r = x < 0 ? s.size() - 1 : s.size();
for (int j = i; j < r; ++j) {
y = y * 10 + s[j] - '0';
if (y > 1e10) {
break;
}
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
if ((x < 0 || x - y == 1) && dfs(dfs, j + 1, y)) {
return true;
}
}
return false;
};
return dfs(0, -1, 0);
return dfs(dfs, 0, -1);
}
};
```
Expand All @@ -156,24 +165,47 @@ public:

```go
func splitString(s string) bool {
var dfs func(i, x, k int) bool
dfs = func(i, x, k int) bool {
if i == len(s) {
return k > 1
var dfs func(i, x int) bool
dfs = func(i, x int) bool {
if i >= len(s) {
return true
}
y := 0
for j := i; j < len(s); j++ {
r := len(s)
if x < 0 {
r--
}
for j := i; j < r; j++ {
y = y*10 + int(s[j]-'0')
if y > int(1e10) {
break
}
if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) {
if (x < 0 || x-y == 1) && dfs(j+1, y) {
return true
}
}
return false
}
return dfs(0, -1, 0)
return dfs(0, -1)
}
```

#### TypeScript

```ts
function splitString(s: string): boolean {
const dfs = (i: number, x: number): boolean => {
if (i >= s.length) {
return true;
}
let y = 0;
const r = x < 0 ? s.length - 1 : s.length;
for (let j = i; j < r; ++j) {
y = y * 10 + +s[j];
if ((x < 0 || x - y === 1) && dfs(j + 1, y)) {
return true;
}
}
return false;
};
return dfs(0, -1);
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
class Solution {
public:
bool splitString(string s) {
function<bool(int, long long, int)> dfs = [&](int i, long long x, int k) -> bool {
if (i == s.size()) {
return k > 1;
auto dfs = [&](auto&& dfs, int i, long long x) -> bool {
if (i >= s.size()) {
return true;
}
long long y = 0;
for (int j = i; j < s.size(); ++j) {
y = y * 10 + (s[j] - '0');
int r = x < 0 ? s.size() - 1 : s.size();
for (int j = i; j < r; ++j) {
y = y * 10 + s[j] - '0';
if (y > 1e10) {
break;
}
if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) {
if ((x < 0 || x - y == 1) && dfs(dfs, j + 1, y)) {
return true;
}
}
return false;
};
return dfs(0, -1, 0);
return dfs(dfs, 0, -1);
}
};
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
func splitString(s string) bool {
var dfs func(i, x, k int) bool
dfs = func(i, x, k int) bool {
if i == len(s) {
return k > 1
var dfs func(i, x int) bool
dfs = func(i, x int) bool {
if i >= len(s) {
return true
}
y := 0
for j := i; j < len(s); j++ {
r := len(s)
if x < 0 {
r--
}
for j := i; j < r; j++ {
y = y*10 + int(s[j]-'0')
if y > int(1e10) {
break
}
if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) {
if (x < 0 || x-y == 1) && dfs(j+1, y) {
return true
}
}
return false
}
return dfs(0, -1, 0)
}
return dfs(0, -1)
}
Loading
Loading