-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |