-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path131.go
More file actions
49 lines (44 loc) · 771 Bytes
/
131.go
File metadata and controls
49 lines (44 loc) · 771 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
package main
import "fmt"
func main() {
fmt.Print(partition("aabaa"))
}
func partition(s string) [][]string {
var cur []string
return getSubstring(s, cur)
}
func getSubstring(s string, cur []string) [][]string {
var res [][]string
if len(s) == 0 {
res = append(res, cur)
return res
}
for i := 1; i < len(s)+1; i++ {
if isPalindrome(s[0:i]) {
var newCur []string
newCur = append(newCur, cur...)
newCur = append(newCur, s[0:i])
res = append(res, getSubstring(s[i:], newCur)...)
}
}
return res
}
func isPalindrome(s string) bool {
if len(s) == 0 {
return false
}
if len(s) == 1 {
return true
}
i, j := 0, len(s)-1
for ; i < j; {
if s[i] == s[j] {
i += 1
j -= 1
continue
} else {
return false
}
}
return true
}