mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
go: add «131. Palindrome Partitioning»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
370c61deb6
commit
98209ffa96
1 changed files with 67 additions and 0 deletions
67
go/palindrome-partitioning.go
Normal file
67
go/palindrome-partitioning.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package palindrome_partitioning
|
||||
|
||||
import (
|
||||
"slices"
|
||||
)
|
||||
|
||||
func getPalindromes(s string) [][]bool {
|
||||
size := len(s) + 1
|
||||
|
||||
// Create a DP table
|
||||
palindromes := make([][]bool, size)
|
||||
for i := range size {
|
||||
palindromes[i] = make([]bool, size)
|
||||
}
|
||||
|
||||
// Set the ranges that represent a palindrome
|
||||
checkPalindromes := func(i, j int) {
|
||||
if i < 0 || j >= len(s) {
|
||||
return
|
||||
}
|
||||
|
||||
for i >= 0 && j < len(s) && s[i] == s[j] {
|
||||
palindromes[i][j+1] = true
|
||||
|
||||
i -= 1
|
||||
j += 1
|
||||
}
|
||||
}
|
||||
for i, _ := range s {
|
||||
checkPalindromes(i, i)
|
||||
checkPalindromes(i, i+1)
|
||||
}
|
||||
|
||||
return palindromes
|
||||
}
|
||||
|
||||
func getPartitions(s string, palindromes [][]bool) [][]string {
|
||||
var partitions [][]string
|
||||
var currentPartition []string
|
||||
|
||||
var dfs func(int)
|
||||
dfs = func(i int) {
|
||||
if i >= len(s) && len(currentPartition) > 0 {
|
||||
partitions = append(partitions, slices.Clone(currentPartition))
|
||||
return
|
||||
} else if i >= len(s) {
|
||||
return
|
||||
}
|
||||
|
||||
for j := len(s); j > i; j-- {
|
||||
if !palindromes[i][j] {
|
||||
continue
|
||||
}
|
||||
|
||||
currentPartition = append(currentPartition, s[i:j])
|
||||
dfs(j)
|
||||
currentPartition = currentPartition[:len(currentPartition)-1]
|
||||
}
|
||||
}
|
||||
|
||||
dfs(0)
|
||||
return partitions
|
||||
}
|
||||
|
||||
func partition(s string) [][]string {
|
||||
return getPartitions(s, getPalindromes(s))
|
||||
}
|
Loading…
Reference in a new issue