-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00131-palindrome_partitioning.go
More file actions
55 lines (44 loc) · 985 Bytes
/
00131-palindrome_partitioning.go
File metadata and controls
55 lines (44 loc) · 985 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
43
44
45
46
47
48
49
50
51
52
53
54
55
// 131: Palindrome Partitioning
// https://leetcode.com/problems/palindrome-partitioning
package main
import "fmt"
func backtrack(s string, start int, current []string, result *[][]string) {
if start == len(s) {
t := make([]string, len(current))
copy(t, current)
*result = append(*result, t)
return
}
for i:=start; i<len(s); i++ {
if (isPalindrome(s, start, i)) {
str := s[start:i+1]
current = append(current, str)
backtrack(s, i + 1, current, result)
current = current[:len(current)-1]
}
}
}
func isPalindrome(s string, start, end int) bool {
for start <= end {
if s[start] != s[end] {
return false
}
start++
end--
}
return true
}
// SOLUTION
func partition(s string) [][]string {
var current []string
var result [][]string
backtrack(s, 0, current, &result)
return result;
}
func main() {
// INPUT
s := "aab"
// OUTPUT
result := partition(s)
fmt.Println(result)
}