Skip to content

Commit

Permalink
Merge cf7e397 into 7519b54
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Sep 30, 2018
2 parents 7519b54 + cf7e397 commit e8df047
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ problems from
* [Day 30](https://github.com/vaskoz/dailycodingproblem-go/issues/69)
* [Day 31](https://github.com/vaskoz/dailycodingproblem-go/issues/71)
* [Day 33](https://github.com/vaskoz/dailycodingproblem-go/issues/73)
* [Day 34](https://github.com/vaskoz/dailycodingproblem-go/issues/76)
* [Day 35](https://github.com/vaskoz/dailycodingproblem-go/issues/77)
* [Day 36](https://github.com/vaskoz/dailycodingproblem-go/issues/79)
* [Day 37](https://github.com/vaskoz/dailycodingproblem-go/issues/81)
Expand Down
49 changes: 49 additions & 0 deletions day34/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package day34

// MakePalindrome calculates the minimum number of insertions to make a string a palindrome.
// Runs in O(N) time and O(N) extra space for favorLeft and favorRight.
func MakePalindrome(str string) string {
letters := []rune(str)
favorLeft := make([]rune, len(letters))
copy(favorLeft, letters)
favorRight := make([]rune, len(letters))
copy(favorRight, letters)
left, right := 0, len(favorLeft)-1
for left < right {
if favorLeft[left] != favorLeft[right] {
// insert the left character on the right side
favorLeft = append(favorLeft, 'x')
copy(favorLeft[right+2:], favorLeft[right+1:])
favorLeft[right+1] = favorLeft[left]
right++
}
left++
right--
}
left, right = 0, len(favorRight)-1
for left < right {
if favorRight[left] != favorRight[right] {
// insert the right character on the left side
favorRight = append(favorRight, 'x')
copy(favorRight[left+1:], favorRight[left:])
favorRight[left] = favorRight[right+1]
right++
}
left++
right--
}
return chooseResult(favorLeft, favorRight)
}

func chooseResult(left, right []rune) string {
if len(left) < len(right) {
return string(left)
} else if len(left) == len(right) {
if left[0] < right[0] {
return string(left)
}
return string(right)
} else {
return string(right)
}
}
30 changes: 30 additions & 0 deletions day34/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day34

import "testing"

var testcases = []struct {
input, expected string
}{
{"race", "ecarace"},
{"google", "elgoogle"},
{"add", "adda"},
{"caoobac", "caboobac"},
{"abc", "abcba"},
}

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

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

0 comments on commit e8df047

Please sign in to comment.