Skip to content

Commit 2ef92a2

Browse files
authored
Merge pull request neetcode-gh#662 from GokhanCagritekin/main
131 palindrome partitioning golang
2 parents 4532559 + 5cb33f7 commit 2ef92a2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

go/131-Palindrome-Partitioning.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
func partition(s string) [][]string {
4+
ans := make([][]string, 0)
5+
curr := make([]string, 0)
6+
var backtrack func(idx int)
7+
backtrack = func(idx int) {
8+
if idx == len(s) {
9+
ans = append(ans, append([]string{}, curr...))
10+
}
11+
for i := idx; i < len(s); i++ {
12+
if isPalindrome(s[idx : i+1]) {
13+
curr = append(curr, s[idx:i+1])
14+
backtrack(i + 1)
15+
curr = curr[:len(curr)-1]
16+
}
17+
}
18+
}
19+
backtrack(0)
20+
return ans
21+
}
22+
23+
func isPalindrome(s string) bool {
24+
l := 0
25+
r := len(s) - 1
26+
for l < r {
27+
if s[l] != s[r] {
28+
return false
29+
}
30+
l++
31+
r--
32+
}
33+
return true
34+
}

0 commit comments

Comments
 (0)