Skip to content

Commit

Permalink
131 palindrome partitioning golang
Browse files Browse the repository at this point in the history
  • Loading branch information
GokhanCagritekin committed Jul 27, 2022
1 parent 52bdc09 commit 5cb33f7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions go/131-Palindrome-Partitioning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

func partition(s string) [][]string {
ans := make([][]string, 0)
curr := make([]string, 0)
var backtrack func(idx int)
backtrack = func(idx int) {
if idx == len(s) {
ans = append(ans, append([]string{}, curr...))
}
for i := idx; i < len(s); i++ {
if isPalindrome(s[idx : i+1]) {
curr = append(curr, s[idx:i+1])
backtrack(i + 1)
curr = curr[:len(curr)-1]
}
}
}
backtrack(0)
return ans
}

func isPalindrome(s string) bool {
l := 0
r := len(s) - 1
for l < r {
if s[l] != s[r] {
return false
}
l++
r--
}
return true
}

0 comments on commit 5cb33f7

Please sign in to comment.