-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q131_PalindromePartitioning.cs
81 lines (73 loc) · 2.36 KB
/
Q131_PalindromePartitioning.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Given a string s, partition s such that every substring of the partition is a palindrome.
// Return all possible palindrome partitioning of s.
// For example, given s = "aab",
// Return
// [
// ["aa","b"],
// ["a","a","b"]
// ]
namespace LeetSharp
{
[TestClass]
public class Q131_PalindromePartitioning
{
public string[][] Partition(string input)
{
List<string[]> result = new List<string[]>();
// dp[i, j] = true -> s[i] to s[j] is palindrome
bool[,] dp = new bool[input.Length, input.Length]; // dp[i,j] means s[i]...s[j] is palindrom or not
for (int s = input.Length - 1; s >= 0; s--)
{
for (int e = s; e < input.Length; e++)
{
if (input[s] == input[e] && (e - s <= 2 || dp[s + 1, e - 1]))
{
dp[s, e] = true;
}
}
}
// then use backtracking to get answer
GenerateAnswer(dp, result, 0, new List<string>(), input);
return result.ToArray();
}
private void GenerateAnswer(bool[,] dp, List<string[]> result, int start, List<string> cur, string input)
{
for (int end = start; end < input.Length; end++)
{
if (dp[start, end])
{
cur.Add(input.Substring(start, end - start + 1));
if (end == input.Length - 1)
{
result.Add(cur.ToArray());
}
else
{
GenerateAnswer(dp, result, end + 1, cur, input);
}
cur.RemoveAt(cur.Count - 1);
}
}
}
public string SolveQuestion(string input)
{
return TestHelper.Serialize(Partition(input.Deserialize()));
}
[TestMethod]
public void Q131_Small()
{
TestHelper.Run(s => SolveQuestion(s));
}
[TestMethod]
public void Q131_Large()
{
TestHelper.Run(s => SolveQuestion(s));
}
}
}