Skip to content
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

fix: update solutions to lc problem: No.1147 #3388

Merged
merged 1 commit into from
Aug 9, 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 @@ -93,107 +93,6 @@ tags:

#### Python3

```python
class Solution:
def longestDecomposition(self, text: str) -> int:
n = len(text)
if n < 2:
return n
for i in range(n // 2 + 1):
if text[:i] == text[-i:]:
return 2 + self.longestDecomposition(text[i:-i])
return 1
```

#### Java

```java
class Solution {
public int longestDecomposition(String text) {
int n = text.length();
if (n < 2) {
return n;
}
for (int i = 1; i <= n >> 1; ++i) {
if (text.substring(0, i).equals(text.substring(n - i))) {
return 2 + longestDecomposition(text.substring(i, n - i));
}
}
return 1;
}
}
```

#### C++

```cpp
class Solution {
public:
int longestDecomposition(string text) {
int n = text.size();
if (n < 2) return n;
for (int i = 1; i <= n >> 1; ++i) {
if (text.substr(0, i) == text.substr(n - i)) {
return 2 + longestDecomposition(text.substr(i, n - i - i));
}
}
return 1;
}
};
```

#### Go

```go
func longestDecomposition(text string) int {
n := len(text)
if n < 2 {
return n
}
for i := 1; i <= n>>1; i++ {
if text[:i] == text[n-i:] {
return 2 + longestDecomposition(text[i:n-i])
}
}
return 1
}
```

#### TypeScript

```ts
function longestDecomposition(text: string): number {
const n: number = text.length;
if (n < 2) {
return n;
}
for (let i: number = 1; i <= n >> 1; i++) {
if (text.slice(0, i) === text.slice(n - i)) {
return 2 + longestDecomposition(text.slice(i, n - i));
}
}
return 1;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:字符串哈希

**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 $0$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。

因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。

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

<!-- tabs:start -->

#### Python3

```python
class Solution:
def longestDecomposition(self, text: str) -> int:
Expand Down Expand Up @@ -343,7 +242,13 @@ function longestDecomposition(text: string): number {

<!-- solution:start -->

### 方法三
### 方法二:字符串哈希

**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 $0$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。

因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,107 +91,6 @@ The time complexity is $O(n^2)$, and the space complexity is $O(n)$ or $O(1)$. H

#### Python3

```python
class Solution:
def longestDecomposition(self, text: str) -> int:
n = len(text)
if n < 2:
return n
for i in range(n // 2 + 1):
if text[:i] == text[-i:]:
return 2 + self.longestDecomposition(text[i:-i])
return 1
```

#### Java

```java
class Solution {
public int longestDecomposition(String text) {
int n = text.length();
if (n < 2) {
return n;
}
for (int i = 1; i <= n >> 1; ++i) {
if (text.substring(0, i).equals(text.substring(n - i))) {
return 2 + longestDecomposition(text.substring(i, n - i));
}
}
return 1;
}
}
```

#### C++

```cpp
class Solution {
public:
int longestDecomposition(string text) {
int n = text.size();
if (n < 2) return n;
for (int i = 1; i <= n >> 1; ++i) {
if (text.substr(0, i) == text.substr(n - i)) {
return 2 + longestDecomposition(text.substr(i, n - i - i));
}
}
return 1;
}
};
```

#### Go

```go
func longestDecomposition(text string) int {
n := len(text)
if n < 2 {
return n
}
for i := 1; i <= n>>1; i++ {
if text[:i] == text[n-i:] {
return 2 + longestDecomposition(text[i:n-i])
}
}
return 1
}
```

#### TypeScript

```ts
function longestDecomposition(text: string): number {
const n: number = text.length;
if (n < 2) {
return n;
}
for (let i: number = 1; i <= n >> 1; i++) {
if (text.slice(0, i) === text.slice(n - i)) {
return 2 + longestDecomposition(text.slice(i, n - i));
}
}
return 1;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: String Hash

**String hash** is to map a string of any length to a non-negative integer, and its collision probability is almost $0$. String hash is used to calculate the hash value of a string and quickly determine whether two strings are equal.

Therefore, based on Solution 1, we can use the method of string hash to compare whether two strings are equal in $O(1)$ time.

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

<!-- tabs:start -->

#### Python3

```python
class Solution:
def longestDecomposition(self, text: str) -> int:
Expand Down Expand Up @@ -341,7 +240,13 @@ function longestDecomposition(text: string): number {

<!-- solution:start -->

### Solution 3
### Solution 2: String Hash

**String hash** is to map a string of any length to a non-negative integer, and its collision probability is almost $0$. String hash is used to calculate the hash value of a string and quickly determine whether two strings are equal.

Therefore, based on Solution 1, we can use the method of string hash to compare whether two strings are equal in $O(1)$ time.

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
class Solution {
public:
int longestDecomposition(string text) {
int n = text.size();
if (n < 2) return n;
for (int i = 1; i <= n >> 1; ++i) {
if (text.substr(0, i) == text.substr(n - i)) {
return 2 + longestDecomposition(text.substr(i, n - i - i));
int ans = 0;
auto check = [&](int i, int j, int k) -> bool {
while (k--) {
if (text[i++] != text[j++]) {
return false;
}
}
return true;
};
for (int i = 0, j = text.size() - 1; i <= j;) {
bool ok = false;
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
if (check(i, j - k + 1, k)) {
ans += 2;
i += k;
j -= k;
ok = true;
break;
}
}
if (!ok) {
ans += 1;
break;
}
}
return 1;
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
func longestDecomposition(text string) int {
n := len(text)
if n < 2 {
return n
}
for i := 1; i <= n>>1; i++ {
if text[:i] == text[n-i:] {
return 2 + longestDecomposition(text[i:n-i])
func longestDecomposition(text string) (ans int) {
for i, j := 0, len(text)-1; i <= j; {
ok := false
for k := 1; i+k-1 < j-k+1; k++ {
if text[i:i+k] == text[j-k+1:j+1] {
ans += 2
i += k
j -= k
ok = true
break
}
}
if !ok {
ans++
break
}
}
return 1
return
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
class Solution {
public int longestDecomposition(String text) {
int n = text.length();
if (n < 2) {
return n;
int ans = 0;
for (int i = 0, j = text.length() - 1; i <= j;) {
boolean ok = false;
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
if (check(text, i, j - k + 1, k)) {
ans += 2;
i += k;
j -= k;
ok = true;
break;
}
}
if (!ok) {
++ans;
break;
}
}
for (int i = 1; i <= n >> 1; ++i) {
if (text.substring(0, i).equals(text.substring(n - i))) {
return 2 + longestDecomposition(text.substring(i, n - i));
return ans;
}

private boolean check(String s, int i, int j, int k) {
while (k-- > 0) {
if (s.charAt(i++) != s.charAt(j++)) {
return false;
}
}
return 1;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
class Solution:
def longestDecomposition(self, text: str) -> int:
n = len(text)
if n < 2:
return n
for i in range(n // 2 + 1):
if text[:i] == text[-i:]:
return 2 + self.longestDecomposition(text[i:-i])
return 1
ans = 0
i, j = 0, len(text) - 1
while i <= j:
k = 1
ok = False
while i + k - 1 < j - k + 1:
if text[i : i + k] == text[j - k + 1 : j + 1]:
ans += 2
i += k
j -= k
ok = True
break
k += 1
if not ok:
ans += 1
break
return ans
Loading
Loading