-
Notifications
You must be signed in to change notification settings - Fork 11
/
ispalindrome.go
55 lines (49 loc) · 1.58 KB
/
ispalindrome.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// ispalindrome.go
// description: Checks if a given string is palindrome or not
// details:
// Palindromes are expressions that read the same way forwards and backwards.
// They can be words/phrases (like "racecar" and "Do geese see God?"), or even
// numbers (like "02/02/2020"). Usually punctuation signs, capitalization
// and spaces are ignored. A regular expression was used to achieve that.
// See more information on: https://en.wikipedia.org/wiki/Palindrome
// author(s) [Fernanda Kawasaki](https://github.com/fernandakawasaki)
// see ispalindrome_test.go
package palindrome
import (
"regexp"
"strings"
)
func cleanString(text string) string {
clean_text := strings.ToLower(text)
clean_text = strings.Join(strings.Fields(clean_text), "") // Remove spaces
regex, _ := regexp.Compile(`[^\p{L}\p{N} ]+`) // Regular expression for alphanumeric only characters
return regex.ReplaceAllString(clean_text, "")
}
func IsPalindrome(text string) bool {
clean_text := cleanString(text)
var i, j int
rune := []rune(clean_text)
for i = 0; i < len(rune)/2; i++ {
j = len(rune) - 1 - i
if string(rune[i]) != string(rune[j]) {
return false
}
}
return true
}
func IsPalindromeRecursive(text string) bool {
clean_text := cleanString(text)
runes := []rune(clean_text)
return isPalindromeRecursiveHelper(runes, 0, int64(len(runes)))
}
func isPalindromeRecursiveHelper(runes []rune, start int64, end int64) bool {
if start >= end {
return true
}
if runes[start] != runes[end-1] {
return false
}
start = start + 1
end = end - 1
return isPalindromeRecursiveHelper(runes, start, end)
}