Valid Palindrome [LeetCode]

HyunMin Yang·2023년 1월 6일
1
post-thumbnail

Problem


Method

1) The question said "alphanumeric", which means every number and alphabet is in it. So we make a list containing all those numbers and alphabets in string format
2) Get an input
3) Use a if statement inside a For loop to check if the element in the string: s is an alphanumeric number and if it is, add that element to the string = ''
4) After the For loop is done, we check if the string and the reversed string is the same
5) If they are same, return True
6) If they are different, return False


LeetCode Private Code

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = s.lower()
        alphanumeric = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9']
        string = ''

        for i in s:
            if i in alphanumeric:
                string += i
        if string == string[::-1]:
            return True
        else:
            return False

Normal Python Code

s = input()
alphanumeric = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9']
string = ''

for i in s:
	if i in alphanumeric:
		string += i
  
if string == string[::-1]:
	print("true")
else:
	print("false")

Result


profile
Hello World!

0개의 댓글