Skip to content

Commit

Permalink
Merge 07e009b into 64a0a04
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 24, 2019
2 parents 64a0a04 + 07e009b commit 1c70f64
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,6 @@ problems from
* [Day 178](https://github.com/vaskoz/dailycodingproblem-go/issues/367)
* [Day 179](https://github.com/vaskoz/dailycodingproblem-go/issues/371)
* [Day 180](https://github.com/vaskoz/dailycodingproblem-go/issues/373)
* [Day 181](https://github.com/vaskoz/dailycodingproblem-go/issues/375)
* [Day 182](https://github.com/vaskoz/dailycodingproblem-go/issues/376)
* [Day 184](https://github.com/vaskoz/dailycodingproblem-go/issues/378)
57 changes: 57 additions & 0 deletions day181/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package day181

// MinimumPartitionPalindrome splits a string into as
// few strings as possible such that each string is a palindrome.
func MinimumPartitionPalindrome(str string) []string {
if len(str) == 0 {
return nil
}
palindrome := make([][]bool, len(str))
for i := range palindrome {
palindrome[i] = make([]bool, len(str))
palindrome[i][i] = true
}
for l := 2; l <= len(str); l++ {
for i := 0; i < len(str)-l+1; i++ {
j := i + l - 1
if l == 2 {
palindrome[i][j] = str[i] == str[j]
} else {
palindrome[i][j] = (str[i] == str[j]) && palindrome[i+1][j-1]
}
}
}
cuts := findCutsBFS(str, palindrome)
result := make([]string, 0, len(cuts))
prev := 0
for _, cut := range cuts {
result = append(result, str[prev:cut])
prev = cut
}
return result
}

func findCutsBFS(s string, pal [][]bool) []int {
var q [][]int
for i, isPal := range pal[0] {
if isPal {
q = append(q, []int{i + 1})
}
}
var cuts []int
for len(q) != 0 {
cuts, q = q[0], q[1:]
start := cuts[len(cuts)-1]
if len(s) == start {
break
}
for i := start; i < len(pal[start]); i++ {
if pal[start][i] {
newCuts := append([]int{}, cuts...)
newCuts = append(newCuts, i+1)
q = append(q, newCuts)
}
}
}
return cuts
}
34 changes: 34 additions & 0 deletions day181/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package day181

import (
"reflect"
"testing"
)

var testcases = []struct {
input string
expected []string
}{
{"racecarannakayak", []string{"racecar", "anna", "kayak"}},
{"abc", []string{"a", "b", "c"}},
{"racecarracecar", []string{"racecarracecar"}},
{"a", []string{"a"}},
{"", nil},
}

func TestMinimumPartitionPalindrome(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := MinimumPartitionPalindrome(tc.input); !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
}
}

func BenchmarkMinimumPartitionPalindrome(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
MinimumPartitionPalindrome(tc.input)
}
}
}

0 comments on commit 1c70f64

Please sign in to comment.