From 07b8dc6b3e3fcb280f47b21eff411683b844c7e2 Mon Sep 17 00:00:00 2001 From: rizzOn Date: Sat, 6 Jun 2026 15:17:19 +0530 Subject: [PATCH] day 5: trying to rizz up gophy mol again --- .DS_Store | Bin 6148 -> 6148 bytes integer_to_roman.go | 3 ++ longest_palindrome_in_a_string.go | 26 +++++++++++++ manachers_algorith_longest_palindrom.go | 47 ++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 longest_palindrome_in_a_string.go create mode 100644 manachers_algorith_longest_palindrom.go diff --git a/.DS_Store b/.DS_Store index 617ec66e574f3ec2b7b851a4ea3ccd6be52e586e..9538566edc8b8b1755a61e27b90ff41060d30962 100644 GIT binary patch delta 282 zcmZoMXfc=|#>B!kF;Q%yo+2aX#(>?7iwl^U7&#~NFg>gnVMu2vV#sGGWhh|CWXJ>Z z;u#WwbRt+hks+6%m_ZN7&iBm8Pfp6oPhwzT5MW?nVgk~7|G@yrVqlO4>y8JiPX*$9 zpqZ6GlZt`7Vld1EisgYtO2D$I5L3fYjb)sKW~?yS*b<;I=|EfrGytSM9_-{?utTvp pj!|jzJtkMC&Fmcf96+CMPGtVhJegm_k%JNFaFAm+M~JLp1^^OCLe>BP delta 67 zcmZoMXfc=|#>B)qF;Q%yo+2aL#(>?7jBJy6SRQV^$ZE#4vET{QW_AvK4xp0Ff*jwO VC-aLqaxee^BLf4=<_M8B%m8fX5Sah~ 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 +}