In this blog post, we will dive into the concept of palindromes and demonstrate how to check if a given string is a palindrome using the Go (Golang) programming language.
A palindrome is a sequence of characters that reads the same forwards and backward, ignoring spaces, punctuation, and capitalization. We will explore different methods to validate palindromes and illustrate their implementation in Go.We can also use two-pointers to compare characters of the original string from both ends. If they match, the string is a palindrome.
Iterative Approach
The most straightforward method to check if a string is a palindrome is through an iterative approach. We can compare characters from both ends of the string and move toward the center until they match.
package main
import (
"fmt"
"strings"
)
func isPalindromeIterative(s string) bool {
// Remove spaces and convert to lowercase
s = strings.ReplaceAll(s, " ", "")
s = strings.ToLower(s)
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-1-i] {
return false
}
}
return true
}
func main() {
str := "Able was I ere I saw Elba" // Change this value to test other strings
if isPalindromeIterative(str) {
fmt.Printf("%s is a palindrome.\n", str)
} else {
fmt.Printf("%s is not a palindrome.\n", str)
}
}
Output:
Able was I ere I saw Elba is a palindrome.
Using Recursion
Another approach to check for a palindrome is through recursion. In this method, a function calls itself to compare characters from the beginning and the end of the string.
package main
import (
"fmt"
"strings"
)
func isPalindromeRecursive(s string) bool {
// Remove spaces and convert to lowercase
s = strings.ReplaceAll(s, " ", "")
s = strings.ToLower(s)
if len(s) <= 1 {
return true
}
if s[0] != s[len(s)-1] {
return false
}
return isPalindromeRecursive(s[1 : len(s)-1])
}
func main() {
str := "A man a plan a canal Panama" // Change this value to test other strings
if isPalindromeRecursive(str) {
fmt.Printf("%s is a palindrome.\n", str)
} else {
fmt.Printf("%s is not a palindrome.\n", str)
}
}
Output:
Able was I ere I saw Elba is a palindrome.
Using Two Pointers
package main
import (
"fmt"
"strings"
)
func isPalindromeTwoPointers(s string) bool {
// Remove spaces and convert to lowercase
s = strings.ReplaceAll(s, " ", "")
s = strings.ToLower(s)
left, right := 0, len(s)-1
for left < right {
if s[left] != s[right] {
return false
}
left++
right--
}
return true
}
func main() {
str := "Madam In Eden, I'm Adam" // Change this value to test other strings
if isPalindromeTwoPointers(str) {
fmt.Printf("%s is a palindrome.\n", str)
} else {
fmt.Printf("%s is not a palindrome.\n", str)
}
}
Output:
Able was I ere I saw Elba is a palindrome.
Conclusion
In this blog post, we explored three different approaches to check if a string is a palindrome using the Go programming language. The iterative approach is the simplest and recommended for most scenarios. The recursive approach provides an elegant solution using function calls, while the two-pointers method offers a more efficient approach.
Understanding and identifying palindromes can be a fun and educational exercise in string manipulation using Golang. Feel free to experiment with different strings and explore further creative ways to validate palindromes. Happy coding in Golang!
Understanding and identifying palindromes can be a fun and educational exercise in string manipulation using Golang. Feel free to experiment with different strings and explore further creative ways to validate palindromes. Happy coding in Golang!
Comments
Post a Comment