Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d5f8fd4

Browse files
fix: update solutions to lc problem: No.1147 (doocs#3388)
No.1147.Longest Chunked Palindrome Decomposition
1 parent e83860f commit d5f8fd4

File tree

16 files changed

+177
-427
lines changed

16 files changed

+177
-427
lines changed

‎solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README.md‎

Lines changed: 7 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -93,107 +93,6 @@ tags:
9393

9494
#### Python3
9595

96-
```python
97-
class Solution:
98-
def longestDecomposition(self, text: str) -> int:
99-
n = len(text)
100-
if n < 2:
101-
return n
102-
for i in range(n // 2 + 1):
103-
if text[:i] == text[-i:]:
104-
return 2 + self.longestDecomposition(text[i:-i])
105-
return 1
106-
```
107-
108-
#### Java
109-
110-
```java
111-
class Solution {
112-
public int longestDecomposition(String text) {
113-
int n = text.length();
114-
if (n < 2) {
115-
return n;
116-
}
117-
for (int i = 1; i <= n >> 1; ++i) {
118-
if (text.substring(0, i).equals(text.substring(n - i))) {
119-
return 2 + longestDecomposition(text.substring(i, n - i));
120-
}
121-
}
122-
return 1;
123-
}
124-
}
125-
```
126-
127-
#### C++
128-
129-
```cpp
130-
class Solution {
131-
public:
132-
int longestDecomposition(string text) {
133-
int n = text.size();
134-
if (n < 2) return n;
135-
for (int i = 1; i <= n >> 1; ++i) {
136-
if (text.substr(0, i) == text.substr(n - i)) {
137-
return 2 + longestDecomposition(text.substr(i, n - i - i));
138-
}
139-
}
140-
return 1;
141-
}
142-
};
143-
```
144-
145-
#### Go
146-
147-
```go
148-
func longestDecomposition(text string) int {
149-
n := len(text)
150-
if n < 2 {
151-
return n
152-
}
153-
for i := 1; i <= n>>1; i++ {
154-
if text[:i] == text[n-i:] {
155-
return 2 + longestDecomposition(text[i:n-i])
156-
}
157-
}
158-
return 1
159-
}
160-
```
161-
162-
#### TypeScript
163-
164-
```ts
165-
function longestDecomposition(text: string): number {
166-
const n: number = text.length;
167-
if (n < 2) {
168-
return n;
169-
}
170-
for (let i: number = 1; i <= n >> 1; i++) {
171-
if (text.slice(0, i) === text.slice(n - i)) {
172-
return 2 + longestDecomposition(text.slice(i, n - i));
173-
}
174-
}
175-
return 1;
176-
}
177-
```
178-
179-
<!-- tabs:end -->
180-
181-
<!-- solution:end -->
182-
183-
<!-- solution:start -->
184-
185-
### 方法二:字符串哈希
186-
187-
**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 0ドル$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。
188-
189-
因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。
190-
191-
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
192-
193-
<!-- tabs:start -->
194-
195-
#### Python3
196-
19796
```python
19897
class Solution:
19998
def longestDecomposition(self, text: str) -> int:
@@ -343,7 +242,13 @@ function longestDecomposition(text: string): number {
343242

344243
<!-- solution:start -->
345244

346-
### 方法三
245+
### 方法二:字符串哈希
246+
247+
**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 0ドル$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。
248+
249+
因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。
250+
251+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
347252

348253
<!-- tabs:start -->
349254

‎solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README_EN.md‎

Lines changed: 7 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -91,107 +91,6 @@ The time complexity is $O(n^2),ドル and the space complexity is $O(n)$ or $O(1)$. H
9191

9292
#### Python3
9393

94-
```python
95-
class Solution:
96-
def longestDecomposition(self, text: str) -> int:
97-
n = len(text)
98-
if n < 2:
99-
return n
100-
for i in range(n // 2 + 1):
101-
if text[:i] == text[-i:]:
102-
return 2 + self.longestDecomposition(text[i:-i])
103-
return 1
104-
```
105-
106-
#### Java
107-
108-
```java
109-
class Solution {
110-
public int longestDecomposition(String text) {
111-
int n = text.length();
112-
if (n < 2) {
113-
return n;
114-
}
115-
for (int i = 1; i <= n >> 1; ++i) {
116-
if (text.substring(0, i).equals(text.substring(n - i))) {
117-
return 2 + longestDecomposition(text.substring(i, n - i));
118-
}
119-
}
120-
return 1;
121-
}
122-
}
123-
```
124-
125-
#### C++
126-
127-
```cpp
128-
class Solution {
129-
public:
130-
int longestDecomposition(string text) {
131-
int n = text.size();
132-
if (n < 2) return n;
133-
for (int i = 1; i <= n >> 1; ++i) {
134-
if (text.substr(0, i) == text.substr(n - i)) {
135-
return 2 + longestDecomposition(text.substr(i, n - i - i));
136-
}
137-
}
138-
return 1;
139-
}
140-
};
141-
```
142-
143-
#### Go
144-
145-
```go
146-
func longestDecomposition(text string) int {
147-
n := len(text)
148-
if n < 2 {
149-
return n
150-
}
151-
for i := 1; i <= n>>1; i++ {
152-
if text[:i] == text[n-i:] {
153-
return 2 + longestDecomposition(text[i:n-i])
154-
}
155-
}
156-
return 1
157-
}
158-
```
159-
160-
#### TypeScript
161-
162-
```ts
163-
function longestDecomposition(text: string): number {
164-
const n: number = text.length;
165-
if (n < 2) {
166-
return n;
167-
}
168-
for (let i: number = 1; i <= n >> 1; i++) {
169-
if (text.slice(0, i) === text.slice(n - i)) {
170-
return 2 + longestDecomposition(text.slice(i, n - i));
171-
}
172-
}
173-
return 1;
174-
}
175-
```
176-
177-
<!-- tabs:end -->
178-
179-
<!-- solution:end -->
180-
181-
<!-- solution:start -->
182-
183-
### Solution 2: String Hash
184-
185-
**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.
186-
187-
Therefore, based on Solution 1, we can use the method of string hash to compare whether two strings are equal in $O(1)$ time.
188-
189-
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the string.
190-
191-
<!-- tabs:start -->
192-
193-
#### Python3
194-
19594
```python
19695
class Solution:
19796
def longestDecomposition(self, text: str) -> int:
@@ -341,7 +240,13 @@ function longestDecomposition(text: string): number {
341240

342241
<!-- solution:start -->
343242

344-
### Solution 3
243+
### Solution 2: String Hash
244+
245+
**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.
246+
247+
Therefore, based on Solution 1, we can use the method of string hash to compare whether two strings are equal in $O(1)$ time.
248+
249+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the string.
345250

346251
<!-- tabs:start -->
347252

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
class Solution {
22
public:
33
int longestDecomposition(string text) {
4-
int n = text.size();
5-
if (n < 2) return n;
6-
for (int i = 1; i <= n >> 1; ++i) {
7-
if (text.substr(0, i) == text.substr(n - i)) {
8-
return 2 + longestDecomposition(text.substr(i, n - i - i));
4+
int ans = 0;
5+
auto check = [&](int i, int j, int k) -> bool {
6+
while (k--) {
7+
if (text[i++] != text[j++]) {
8+
return false;
9+
}
10+
}
11+
return true;
12+
};
13+
for (int i = 0, j = text.size() - 1; i <= j;) {
14+
bool ok = false;
15+
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
16+
if (check(i, j - k + 1, k)) {
17+
ans += 2;
18+
i += k;
19+
j -= k;
20+
ok = true;
21+
break;
22+
}
23+
}
24+
if (!ok) {
25+
ans += 1;
26+
break;
927
}
1028
}
11-
return 1;
29+
return ans;
1230
}
1331
};
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
func longestDecomposition(text string) int {
2-
n := len(text)
3-
if n < 2 {
4-
return n
5-
}
6-
for i := 1; i <= n>>1; i++ {
7-
if text[:i] == text[n-i:] {
8-
return 2 + longestDecomposition(text[i:n-i])
1+
func longestDecomposition(text string) (ans int) {
2+
for i, j := 0, len(text)-1; i <= j; {
3+
ok := false
4+
for k := 1; i+k-1 < j-k+1; k++ {
5+
if text[i:i+k] == text[j-k+1:j+1] {
6+
ans += 2
7+
i += k
8+
j -= k
9+
ok = true
10+
break
11+
}
12+
}
13+
if !ok {
14+
ans++
15+
break
916
}
1017
}
11-
return1
18+
return
1219
}
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
class Solution {
22
public int longestDecomposition(String text) {
3-
int n = text.length();
4-
if (n < 2) {
5-
return n;
3+
int ans = 0;
4+
for (int i = 0, j = text.length() - 1; i <= j;) {
5+
boolean ok = false;
6+
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
7+
if (check(text, i, j - k + 1, k)) {
8+
ans += 2;
9+
i += k;
10+
j -= k;
11+
ok = true;
12+
break;
13+
}
14+
}
15+
if (!ok) {
16+
++ans;
17+
break;
18+
}
619
}
7-
for (int i = 1; i <= n >> 1; ++i) {
8-
if (text.substring(0, i).equals(text.substring(n - i))) {
9-
return 2 + longestDecomposition(text.substring(i, n - i));
20+
return ans;
21+
}
22+
23+
private boolean check(String s, int i, int j, int k) {
24+
while (k-- > 0) {
25+
if (s.charAt(i++) != s.charAt(j++)) {
26+
return false;
1027
}
1128
}
12-
return 1;
29+
return true;
1330
}
1431
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
class Solution:
22
def longestDecomposition(self, text: str) -> int:
3-
n = len(text)
4-
if n < 2:
5-
return n
6-
for i in range(n // 2 + 1):
7-
if text[:i] == text[-i:]:
8-
return 2 + self.longestDecomposition(text[i:-i])
9-
return 1
3+
ans = 0
4+
i, j = 0, len(text) - 1
5+
while i <= j:
6+
k = 1
7+
ok = False
8+
while i + k - 1 < j - k + 1:
9+
if text[i : i + k] == text[j - k + 1 : j + 1]:
10+
ans += 2
11+
i += k
12+
j -= k
13+
ok = True
14+
break
15+
k += 1
16+
if not ok:
17+
ans += 1
18+
break
19+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /