leetcode.com This is the practice of coding interviews for software developers. A total of more than 1,500 coding questions have been posted, and it seems that the same questions are often asked in actual interviews.
Introduction to golang + algorithm I will solve it with go and Python to strengthen the brain. (Python is weak but experienced)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Japanese translation
Specify a string to consider only alphanumeric characters and ignore uppercase and lowercase letters to determine if it is a palindrome.
** Note: ** For the purposes of this issue, define an empty string as a valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Remove whitespace from the given string to make it an all-lowercase string
If the slice is the same as the one seen from the opposite side, it is true. If it is different, it is false.
Answer code
class Solution:
def isPalindrome(self, s):
s = ''.join(e for e in s if e.isalnum()).lower()
return s==s[::-1]
--I'll write it in Go too!
import (
"regexp"
"strings"
)
func isPalindrome(s string) bool {
reg, _ := regexp.Compile("[^a-zA-Z0-9]+")
processedString := reg.ReplaceAllString(s, "")
processedString = strings.ToLower(processedString)
for i := 0; i < len(processedString)/2; i++ {
if processedString[i] != processedString[len(processedString)-1-i] {
return false
}
}
return true
}
Recommended Posts