知之者 不如好之者, 好之者 不如樂之者

기계처럼 살지 말고, 즐기는 인간이 되자

Code/LeetCode

[LeetCode] 125. Valid Palindrome (Easy/Python)

코방코 2022. 9. 25. 07:05
728x90
 

Valid Palindrome - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Above is the link to the problem.

 

Problem

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 = "0P"
Output: false
Explanation: "0P" 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.

 

Example 4:

Input: s = "a"
Output: true
Explanation: "a" is a palindrome.

 

I solved this problem by the below code.

 

 

 

 

When I first saw this problem, I want to solve this by Stack : Last In First Out

But there need more For loop for using stack structure.

So I solved this problem just slicing and reversing by function "reversed"

 

This code get the top 12.04% of time efficiency and use 15.6 MB of memory.

 

728x90
반응형