algos_and_structures/algocode/two_pointers/valid_palindrome.py
2026-02-07 09:19:45 +03:00

44 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
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 = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.
"""
"""
Инициализируем i - первый элемент (символ строки), j - последний
Далее будем каждый шаг двигать их к друг другу, пока i > j
По ходу будем:
1. Убеждаться что у нас действительная буква (не символы всякие по типу запятых, пробелов и т.д)
1.1 Если попадается что-то не валидное - двигаем указатель дальше
2. Как только убедились что пришел валидный символ - делаем его lowercase
3. Срваниваем
A man, a plan, a canal: Panama
i j
"""
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return