-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome-partitioning.cs
56 lines (48 loc) · 1.94 KB
/
palindrome-partitioning.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
// https://leetcode.com/problems/palindrome-partitioning/
using System;
public class Solution {
Dictionary<string,bool> pal;
public IList<IList<string>> Partition(string s) {
Dictionary<string,IList<IList<string>>> solution = new Dictionary<string,IList<IList<string>>>();
solution[""] = (IList<IList<string>>)(new List<IList<string>>(){
(IList<string>)(new List<string>(){})
});
for(int i=1;i<=s.Length;i++){
//Console.WriteLine("i={0}",i);
for(int j=0;j<s.Length && (i+j)<=s.Length;j++){
//Console.WriteLine(" j={0}",j);
string substring = s.Substring(j,i);
solution[substring]= new List<IList<string>>(){};
for(int k=1; k<substring.Length;k++){
//Console.WriteLine(" k={0}",k);
string newp = substring.Substring(0,k);
if(ispalindrome(newp)){
foreach(IList<string> way in solution[substring.Substring(k, substring.Length-k)]){
List<string> toAdd = new List<string>(){newp};
toAdd.AddRange(way);
solution[substring].Add((IList<string>)toAdd);
}
}
}
if(ispalindrome(substring)) solution[substring].Add((IList<string>)(new List<string>(){substring}));
solution[substring] = (IList<IList<string>>)solution[substring];
}
}
return solution[s];
}
public Solution(){
pal = new Dictionary<string, bool>();
}
public bool ispalindrome(string s){
if(pal.ContainsKey(s)) return pal[s];
bool answer = true;
for(int i=0; i<s.Length/2; i++){
if(s[i]!=s[s.Length-1-i]){
answer=false;
break;
}
}
pal.Add(s,answer);
return answer;
}
}