-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem034.go
More file actions
33 lines (28 loc) · 808 Bytes
/
Copy pathproblem034.go
File metadata and controls
33 lines (28 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package problem034
import (
"fmt"
)
func minString(s1 string, s2 string) string {
if s1 <= s2 {
return s1
}
return s2
}
func ClosestPalindrome(word string) string {
if len(word) <= 1 {
return word
}
firstCharacter, lastCharacter := word[0], word[len(word)-1]
if firstCharacter == lastCharacter {
return fmt.Sprintf("%c%s%c", firstCharacter, ClosestPalindrome(word[1:len(word)-1]), lastCharacter)
}
candidate1 := fmt.Sprintf("%c%s%c", firstCharacter, ClosestPalindrome(word[1:]), firstCharacter)
candidate2 := fmt.Sprintf("%c%s%c", lastCharacter, ClosestPalindrome(word[:len(word)-1]), lastCharacter)
if len(candidate1) < len(candidate2) {
return candidate1
} else if len(candidate1) == len(candidate2) {
return minString(candidate1, candidate2)
} else {
return candidate2
}
}