SprintCode.pro

Подготовка к алгоритмическим задачам

Super

Valid Palindrome

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "Was it a car or a cat I saw?"

Output: true

Explanation: After removing spaces and converting to lowercase, we get "wasitacaroracatisaw", 
which is a palindrome.

Example 2:

Input: s = "Hello World"

Output: false

Explanation: After processing, we get "helloworld", which is not a palindrome.

Recommended time and space complexity

You should aim for a solution with O(n) time complexity and O(1) space complexity, where n is the length of the input string.


Hint 1

First, you need to clean the string by removing all characters except letters and digits, and convert it to a single case.


Hint 2

Instead of creating a new string, you can use two pointers (at the beginning and end) and move towards each other, skipping unnecessary characters.


Hint 3

You can use built-in methods to check if a character is a letter or digit, or create your own helper function for this.

This problem tests skills in working with strings, their transformation and comparison. It helps develop text processing skills and understanding of palindrome concepts. The solution includes working with regular expressions or direct character processing, which is useful for improving string manipulation skills.

Expected Input :

"Was it a car or a cat I saw?"

Expected Output

true