Scala Palindrome Check
object PalindromeChecker { def isPalindrome(s: String): Boolean = { val cleaned = s.toLowerCase.replaceAll("[^a-z0-9]", "") if (cleaned.isEmpty) { return true } var left = 0 var right = cleaned.length - 1 while (left < right) { if (cleaned(left) != cleaned(right)) { return false...
This Scala function determines if a string is a palindrome, meaning it reads the same forwards and backward. It first cleans the input string by converting it to lowercase and removing all non-alphanumeric characters. Th...
The `isPalindrome` function first cleans the input string `s` by converting it to lowercase and removing any characters that are not letters or numbers using `toLowerCase` and `replaceAll`. An empty cleaned string is considered a palindrome. It then uses two pointers, `left` starting at the beginning and `right` at the end of the cleaned string. The `while` loop continues as long as `left` is less than `right`. Inside the loop, it compares the characters at `left` and `right`. If they don't match, the string is not a palindrome, and `false` is returned. If they match, the pointers are moved inwards (`left` increments, `right` decrements). If the loop completes without finding mismatches, the string is a palindrome, and `true` is returned. Time complexity is O(n) due to cleaning and traversal, where n is the length of the original string. Space complexity is O(n) in the worst case for the cleaned string.
function isPalindrome(s): cleaned_s = toLowerCase(s) cleaned_s = removeNonAlphanumeric(cleaned_s) left = 0 right = length(cleaned_s) - 1 while left < right: if cleaned_s[left] != cleaned_s[right]: return false left = lef...