Drill

ProblemsGo › warmup

Palindrome check

easywarmupStringsTwo pointersGo

A puzzle feature checks whether a phrase reads the same backwards.

isPalindrome(text: string) → bool

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func isPalindrome(text string) bool {
	
}

Worked examples

CallResult
isPalindrome("")true
isPalindrome("racecar")true
isPalindrome("A man, a plan, a canal: Panama")true
isPalindrome("hello")false

Hint

Walk one index from each end, skipping anything that is not alphanumeric.

Reference solution in Go
func isPalindrome(text string) bool {
	clean := []rune{}
	for _, c := range strings.ToLower(text) {
		if (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') {
			clean = append(clean, c)
		}
	}
	for i, j := 0, len(clean)-1; i < j; i, j = i+1, j-1 {
		if clean[i] != clean[j] {
			return false
		}
	}
	return true
}

The same problem in another language

More warmup problems in Go