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 ecc2f8f

Browse files
authored
Create Solution.java
1 parent e090c2e commit ecc2f8f

File tree

1 file changed

+32
-0
lines changed
  • solution/1147.Longest Chunked Palindrome Decomposition

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
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+
}
20+
}
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;
28+
}
29+
}
30+
return true;
31+
}
32+
}

0 commit comments

Comments
(0)

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