Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b13602

Browse files
Valid-Palindrome
1 parent 3806b53 commit 2b13602

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

‎Strings/isvalidPalindrome.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ def isPalindrome(self, s):
2020
return True
2121
else:
2222
return False
23+
24+
#T:O(N)
25+
#S:O(N)

‎Two-pointers/validpalindrome.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"Leetcode - https://leetcode.com/problems/valid-palindrome/"
2+
'''
3+
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.
4+
5+
Given a string s, return true if it is a palindrome, or false otherwise.
6+
7+
Example 1:
8+
9+
Input: s = "A man, a plan, a canal: Panama"
10+
Output: true
11+
Explanation: "amanaplanacanalpanama" is a palindrome.
12+
'''
13+
def isPalindrome(self, s: str) -> bool:
14+
i=0
15+
j=len(s)-1
16+
17+
while i<j:
18+
while i<j and not s[i].isalnum():
19+
i+=1
20+
while i<j and not s[j].isalnum():
21+
j-=1
22+
if s[i].lower() != s[j].lower():
23+
return False
24+
25+
i+=1
26+
j-=1
27+
return True
28+
29+
#T:O(N)
30+
#S:O(1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /