diff --git a/.DS_Store b/.DS_Store index 617ec66..9538566 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/integer_to_roman.go b/integer_to_roman.go index 48722eb..aff8bad 100644 --- a/integer_to_roman.go +++ b/integer_to_roman.go @@ -61,3 +61,6 @@ func intToRomanOne(num int) string { // then a single allocation is made of the exact size needed for the result. return r3[num%1e4/1e3] + r2[num%1e3/1e2] + r1[num%100/10] + r0[num%10] } + + + diff --git a/longest_palindrome_in_a_string.go b/longest_palindrome_in_a_string.go new file mode 100644 index 0000000..d68898c --- /dev/null +++ b/longest_palindrome_in_a_string.go @@ -0,0 +1,26 @@ +package gopherriz + +func longestPalindromeAlternative(s string) string { + if len(s) < 2 { + return s + } + + start, maxLength := 0, 1 + expand := func(l, i int) { + for l >= 0 && i < len(s) && s[l] == s[i] { + if i-l+1 > maxLength { + start = l + maxLength = i - l + 1 + } + l = l - 1 + i = i + 1 + } + } + + for i, _ := range s { + expand(i, i) + expand(i, i+1) + } + + return s[start : start+maxLength] +} diff --git a/manachers_algorith_longest_palindrom.go b/manachers_algorith_longest_palindrom.go new file mode 100644 index 0000000..3e6d68c --- /dev/null +++ b/manachers_algorith_longest_palindrom.go @@ -0,0 +1,47 @@ +package gopherriz + +func longestPalindrome(s string) string { + transformed := "#" + for _, letter := range s { + transformed += string(letter) + "#" + } + + n := len(transformed) + p := make([]int, n) + c, r := 0, 0 + mirror := 0 + for i := 0; i < n; i++ { + mirror = c*2 - i + if i < r { + p[i] = min(r-i, p[mirror]) + } + + for i+p[i]+1 < n && i-p[i]-1 >= 0 && transformed[i+p[i]+1] == transformed[i-p[i]-1] { + p[i] += 1 + } + + if i+p[i] > r { + c = i + r = c + p[c] + } + } + + maxLength, centre := 0, 0 + for i, v := range p { + if v > maxLength { + maxLength = v + centre = i + } + } + + start := (centre - maxLength) / 2 + return s[start : start+maxLength] + +} + +func min(a int, b int) int { + if a < b { + return a + } + return b +}