Valid Palindrome

by atalaykutlay

Solution for Valid Palindrome question on Leetcode

Valid Palindrome
1class Solution:
2    def isPalindrome(self, s: str) -> bool:
3        s = [char for char in s.lower() if char in string.ascii_lowercase or char in string.digits]
4        for i in range(0, len(s)//2):
5            if s[i] != s[-i-1]:
6                return False
7        return True

✨ Code Explanation ✨

This is a class Solution with a method isPalindrome that takes a string s as input and returns a boolean value indicating whether or not the input string s is a palindrome. The method first converts the input string s to a list s by iterating through each character in s with the help of a for loop with a list comprehension inside. The characters are converted to lowercase and only alphanumeric characters (lowercase letters and digits) are included in the list s with the help of the string.ascii_lowercase and string.digits constants. Then, the method uses a for loop that iterates through the first half of the list s, comparing each pair of characters from the opposite ends of the list. If the characters are not equal, the method returns False indicating that the input string is not a palindrome. Otherwise, if all pairs of characters are equal, the method returns True indicating that the input string is a palindrome.

Share

Share
Video
A cool 10 sec video.
Share
Detailed
Title, description, and syntax highlighted code.
Share
Simple
Syntax highlighted code with gradient background colored presentation.

Comments

It looks like there is no comment for this snippet. Leave the first one!
You need to login to send a comment.

Similar Snippets

Count Number of Texts
Count Number of Texts

Solution to "Count Number of Texts" question on Leetcode.

Language: python12 months ago
Quicksort in Python
Quicksort in Python

An easy quicksort implementation in Python.

Language: python13 months ago
Second Minimum Node In a Binary Tree
Second Minimum Node In a Binary Tree

Solution to "Second Minimum Node In a Binary Tree" on Leetcode.

Language: python11 months ago