Complete the code to check if a string is a palindrome by comparing characters from start and end.
func isPalindrome(s string) bool {
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-1-[1]] {
return false
}
}
return true
}The index from the end is calculated as len(s)-1-i to compare characters from both ends.
Complete the code to convert a string to lowercase before checking palindrome.
import "strings" func isPalindrome(s string) bool { s = strings.[1](s) for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true }
Using strings.ToLower converts the string to lowercase, ensuring case-insensitive palindrome check.
Fix the error in the code to correctly ignore non-alphanumeric characters when checking palindrome.
import ( "strings" "unicode" ) func isPalindrome(s string) bool { filtered := "" for _, r := range s { if unicode.IsLetter(r) || unicode.IsDigit(r) { filtered += strings.ToLower(string([1])) } } for i := 0; i < len(filtered)/2; i++ { if filtered[i] != filtered[len(filtered)-1-i] { return false } } return true }
The variable r represents the current rune (character) in the loop and should be converted to lowercase and added.
Fill both blanks to create a map of characters and their counts from a string.
func charCountMap(s string) map[rune]int {
counts := make(map[rune]int)
for _, [1] := range s {
counts[[2]]++
}
return counts
}The loop variable ch represents each character, which is used as the key in the map to count occurrences.
Fill all three blanks to create a palindrome check ignoring case and non-alphanumeric characters.
import ( "strings" "unicode" ) func isPalindrome(s string) bool { filtered := "" for _, [1] := range s { if unicode.IsLetter([2]) || unicode.IsDigit([3]) { filtered += strings.ToLower(string(ch)) } } for i := 0; i < len(filtered)/2; i++ { if filtered[i] != filtered[len(filtered)-1-i] { return false } } return true }
Use the same variable ch to represent the current character in the loop and in the checks.
