Pythin simple leetcode question, IndexError: string index out of range

  Kiến thức lập trình

Hello new to python not new to programming,

I am working on this leetcode question:
https://leetcode.com/problems/valid-palindrome/

and my code is in Python 3:

class Solution:
    def isPalindrome(self, s: str) -> bool:
        palinString = s.replace (" ", "")
        palinString = re.sub(r'[^a-zA-Z]', '', palinString)
        palinString = palinString.lower()
        print(palinString[0])

I am getting:

IndexError: string index out of range
~~~~~~~~~~~^^^
print(palinString[0]) Line 6 in isPalindrome (Solution.py)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ret = Solution().isPalindrome(param_1) Line 28 in _driver (Solution.py)
_driver() Line 39 in (Solution.py)

Any help would be appreciated as sometimes accessing the first element works.

Thank you.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT