-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1147.cs
45 lines (38 loc) · 1.08 KB
/
Solution1147.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1147
{
/// <summary>
/// 1147. Longest Chunked Palindrome Decomposition - Hard
/// <a href="https://leetcode.com/problems/longest-chunked-palindrome-decomposition">See the problem</a>
/// </summary>
public int LongestDecomposition(string text)
{
int left = 0, right = text.Length - 1;
int k = 0;
while (left <= right)
{
var matched = false;
for (int len = 1; left + len - 1 < right - len + 1; len++)
{
var prefix = text.Substring(left, len);
var suffix = text.Substring(right - len + 1, len);
if (prefix == suffix)
{
k += 2;
left += len;
right -= len;
matched = true;
break;
}
}
if (!matched)
{
k += 1;
break;
}
}
return k;
}
}