-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpartition.go
More file actions
42 lines (36 loc) · 830 Bytes
/
Copy pathpartition.go
File metadata and controls
42 lines (36 loc) · 830 Bytes
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
/* https://leetcode.com/problems/palindrome-partitioning/description/
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"]
]
*/
package lbacktracking
func partition(s string) [][]string {
res := [][]string{}
for i := len(s); i >= 1; i-- {
if tmpS := s[0:i]; partitionHelper(tmpS) {
if i == len(s) {
res = append(res, []string{tmpS})
continue
}
if subs := partition(s[i:]); len(subs) != 0 {
for _, sub := range subs {
res = append(res, append([]string{tmpS}, sub...))
}
}
}
}
return res
}
func partitionHelper(s string) bool {
for i := 0; i <= len(s)/2; i++ {
if s[i] != s[len(s)-1-i] {
return false
}
}
return true
}