-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome.go
49 lines (40 loc) · 853 Bytes
/
Palindrome.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
package main
import (
"fmt"
"unicode"
)
func isPalindrome(input string) bool {
//base-case zero & 1 lenght input
inputlength := len(input)
if inputlength == 0 || inputlength == 1 {
return true
}
//invalidate palindrom assumption
start := 0
end := inputlength - 1
for start < end {
//get these as runes - golangs data-type type to represent Unicode
lr, hr := rune(input[start]), rune(input[end])
//move start end end inwards
//invalid chars
if !unicode.IsLetter(lr) {
start++
continue
}
if !unicode.IsLetter(hr) {
end--
continue
}
//check equality
if unicode.ToLower(lr) != unicode.ToLower(hr) {
return false
}
//advance
start++
end--
}
return true
}
func main() {
fmt.Printf("race car! - %t\n", isPalindrome("race car!"))
}