Longestpalindrome

// longestpalindrome.go
// description: Manacher's algorithm (Longest palindromic substring)
// details:
// An algorithm with linear running time that allows you to get compressed information about all palindromic substrings of a given string. - [Manacher's algorithm](https://en.wikipedia.org/wiki/Longest_palindromic_substring)
// author(s) [red_byte](https://github.com/i-redbyte)
// see longestpalindrome_test.go

package manacher

import (
	"github.com/TheAlgorithms/Go/math/min"
	"strings"
)

func makeBoundaries(s string) string {
	var result strings.Builder
	result.WriteRune('#')
	for _, ch := range s {
		if ch != ' ' { //ignore space as palindrome character
			result.WriteRune(ch)
		}
		result.WriteRune('#')
	}
	return result.String()
}

func nextBoundary(s string) string {
	var result strings.Builder
	for _, ch := range s {
		if ch != '#' {
			result.WriteRune(ch)
		}
	}
	return result.String()
}

func LongestPalindrome(s string) string {
	boundaries := makeBoundaries(s)
	b := make([]int, len(boundaries))
	k := 0
	index := 0
	maxLen := 0
	maxCenterSize := 0
	for i := range b {
		if i < k {
			b[i] = min.Int(b[2*index-i], k-i)
		} else {
			b[i] = 1
		}
		for i-b[i] >= 0 && i+b[i] < len(boundaries) && boundaries[i-b[i]] == boundaries[i+b[i]] {
			b[i] += 1
		}
		if maxLen < b[i]-1 {
			maxLen = b[i] - 1
			maxCenterSize = i
		}
		if b[i]+i-1 > k {
			k = b[i] + i - 1
			index = i
		}
	}
	return nextBoundary(boundaries[maxCenterSize-maxLen : maxCenterSize+maxLen])
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.