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.
'Code > LeetCode' 카테고리의 다른 글
[LeetCode] 168. Excel Sheet Column Title (Easy/Python) (1) | 2022.09.29 |
---|---|
[LeetCode] 136. Single number (Easy/Python) (0) | 2022.09.26 |
[LeetCode] 121. Best Time to Buy and Sell Stock (Easy/Python) (1) | 2022.09.23 |
[LeetCode] 88. Merge Sorted Array (Easy/Python) (0) | 2022.09.17 |
[LeetCode] 70. Climbing Stairs (Easy/Python) (0) | 2022.09.15 |