Skip to content

Commit 1df504d

Browse files
committed
feat: add solutions to lc problem: No.1147
No.1147.Longest Chunked Palindrome Decomposition
1 parent 4b145c4 commit 1df504d

File tree

8 files changed

+238
-28
lines changed

8 files changed

+238
-28
lines changed

solution/1000-1099/1044.Longest Duplicate Substring/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151

5252
对于本题,二分枚举长度,找到满足条件的最大长度即可。
5353

54+
时间复杂度 $O(n\log n)$。其中 $n$ 为字符串长度。
55+
56+
相似题目:[1062. 最长重复子串](/solution/1000-1099/1062.Longest%20Repeating%20Substring/README.md)
57+
5458
<!-- tabs:start -->
5559

5660
### **Python3**

solution/1000-1099/1062.Longest Repeating Substring/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ $$
6767

6868
其中 $n$ 为字符串 $s$ 的长度。
6969

70+
相似题目:[1044. 最长重复子串](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README.md)
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,119 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:贪心 + 递归**
59+
60+
从字符串的两端开始,如果两端的字符相同,则可以贪心地将这两端的字符作为一段回文串,然后递归处理中间的字符串。
61+
62+
时间复杂度 $O(n^2)$,其中 $n$ 为字符串的长度。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
6167

6268
<!-- 这里可写当前语言的特殊实现逻辑 -->
6369

6470
```python
65-
71+
class Solution:
72+
def longestDecomposition(self, text: str) -> int:
73+
n = len(text)
74+
if n < 2:
75+
return n
76+
for i in range(n // 2 + 1):
77+
if text[:i] == text[-i:]:
78+
return 2 + self.longestDecomposition(text[i: -i])
79+
return 1
6680
```
6781

6882
### **Java**
6983

7084
<!-- 这里可写当前语言的特殊实现逻辑 -->
7185

7286
```java
87+
class Solution {
88+
public int longestDecomposition(String text) {
89+
int n = text.length();
90+
if (n < 2) {
91+
return n;
92+
}
93+
for (int i = 1; i <= n >> 1; ++i) {
94+
if (text.substring(0, i).equals(text.substring(n - i))) {
95+
return 2 + longestDecomposition(text.substring(i, n - i));
96+
}
97+
}
98+
return 1;
99+
}
100+
}
101+
```
102+
103+
```java
104+
class Solution {
105+
public int longestDecomposition(String text) {
106+
char[] cs = text.toCharArray();
107+
int res = 0;
108+
for (int i = 0, j = cs.length - 1; i <= j;) {
109+
boolean flag = true;
110+
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
111+
if (check(cs, i, j - k + 1, k)) {
112+
res += 2;
113+
i += k;
114+
j -= k;
115+
flag = false;
116+
break;
117+
}
118+
}
119+
if (flag) {
120+
++res;
121+
break;
122+
}
123+
}
124+
return res;
125+
}
126+
127+
private boolean check(char[] cs, int i, int j, int k) {
128+
while (k-- > 0) {
129+
if (cs[i++] != cs[j++]) {
130+
return false;
131+
}
132+
}
133+
return true;
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
int longestDecomposition(string text) {
144+
int n = text.size();
145+
if (n < 2) return n;
146+
for (int i = 1; i <= n >> 1; ++i) {
147+
if (text.substr(0, i) == text.substr(n - i)) {
148+
return 2 + longestDecomposition(text.substr(i, n - i - i));
149+
}
150+
}
151+
return 1;
152+
}
153+
};
154+
```
73155
156+
### **Go**
157+
158+
```go
159+
func longestDecomposition(text string) int {
160+
n := len(text)
161+
if n < 2 {
162+
return n
163+
}
164+
for i := 1; i <= n>>1; i++ {
165+
if text[:i] == text[n-i:] {
166+
return 2 + longestDecomposition(text[i:n-i])
167+
}
168+
}
169+
return 1
170+
}
74171
```
75172

76173
### **...**

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,104 @@
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def longestDecomposition(self, text: str) -> int:
59+
n = len(text)
60+
if n < 2:
61+
return n
62+
for i in range(n // 2 + 1):
63+
if text[:i] == text[-i:]:
64+
return 2 + self.longestDecomposition(text[i: -i])
65+
return 1
5866
```
5967

6068
### **Java**
6169

6270
```java
71+
class Solution {
72+
public int longestDecomposition(String text) {
73+
int n = text.length();
74+
if (n < 2) {
75+
return n;
76+
}
77+
for (int i = 1; i <= n >> 1; ++i) {
78+
if (text.substring(0, i).equals(text.substring(n - i))) {
79+
return 2 + longestDecomposition(text.substring(i, n - i));
80+
}
81+
}
82+
return 1;
83+
}
84+
}
85+
```
86+
87+
```java
88+
class Solution {
89+
public int longestDecomposition(String text) {
90+
char[] cs = text.toCharArray();
91+
int res = 0;
92+
for (int i = 0, j = cs.length - 1; i <= j;) {
93+
boolean flag = true;
94+
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
95+
if (check(cs, i, j - k + 1, k)) {
96+
res += 2;
97+
i += k;
98+
j -= k;
99+
flag = false;
100+
break;
101+
}
102+
}
103+
if (flag) {
104+
++res;
105+
break;
106+
}
107+
}
108+
return res;
109+
}
110+
111+
private boolean check(char[] cs, int i, int j, int k) {
112+
while (k-- > 0) {
113+
if (cs[i++] != cs[j++]) {
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int longestDecomposition(string text) {
128+
int n = text.size();
129+
if (n < 2) return n;
130+
for (int i = 1; i <= n >> 1; ++i) {
131+
if (text.substr(0, i) == text.substr(n - i)) {
132+
return 2 + longestDecomposition(text.substr(i, n - i - i));
133+
}
134+
}
135+
return 1;
136+
}
137+
};
138+
```
63139
140+
### **Go**
141+
142+
```go
143+
func longestDecomposition(text string) int {
144+
n := len(text)
145+
if n < 2 {
146+
return n
147+
}
148+
for i := 1; i <= n>>1; i++ {
149+
if text[:i] == text[n-i:] {
150+
return 2 + longestDecomposition(text[i:n-i])
151+
}
152+
}
153+
return 1
154+
}
64155
```
65156

66157
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
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));
9+
}
10+
}
11+
return 1;
12+
}
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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])
9+
}
10+
}
11+
return 1
12+
}
Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
class Solution {
22
public int longestDecomposition(String text) {
3-
char[] cs = text.toCharArray();
4-
int res = 0;
5-
for (int i = 0, j = cs.length - 1; i <= j;) {
6-
boolean flag = true;
7-
for (int k = 1; i + k - 1 < j - k + 1; ++k) {
8-
if (check(cs, i, j - k + 1, k)) {
9-
res += 2;
10-
i += k;
11-
j -= k;
12-
flag = false;
13-
break;
14-
}
15-
}
16-
if (flag) {
17-
++res;
18-
break;
19-
}
3+
int n = text.length();
4+
if (n < 2) {
5+
return n;
206
}
21-
return res;
22-
}
23-
24-
private boolean check(char[] cs, int i, int j, int k) {
25-
while (k-- > 0) {
26-
if (cs[i++] != cs[j++]) {
27-
return false;
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));
2810
}
2911
}
30-
return true;
12+
return 1;
3113
}
32-
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
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

0 commit comments

Comments
 (0)