lc125. Valid Palindrome
Contents
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:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints:
1 <= s.length <= 2 * 105
s
consists only of printable ASCII characters.
解题思路
- 把字符串转成小写
- 首先第一个循环只保留字符串中的字母和数字
- 利用指针双头判断。
|
|
这道题注意一下valid字符串别漏写就行。因为我们只需要遍历一遍字符串,所以时间复杂度为O(N),同时空间复杂度也为O(N)