Skip to content

Commit 773c2e2

Browse files
authored
Create 09-00125-valid-palindrome.py
1 parent e2d853a commit 773c2e2

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters,
2+
# it reads the same forward and backward. Alphanumeric characters include letters and numbers.
3+
4+
# Given a string s, return true if it is a palindrome, or false otherwise.
5+
6+
# Example 1:
7+
# Input: s = "A man, a plan, a canal: Panama"
8+
# Output: true
9+
# Explanation: "amanaplanacanalpanama" is a palindrome.
10+
11+
# Example 2:
12+
# Input: s = "race a car"
13+
# Output: false
14+
# Explanation: "raceacar" is not a palindrome.
15+
16+
# Example 3:
17+
# Input: s = " "
18+
# Output: true
19+
# Explanation: s is an empty string "" after removing non-alphanumeric characters.
20+
# Since an empty string reads the same forward and backward, it is a palindrome.
21+
22+
# Constraints:
23+
# 1 <= s.length <= 2 * 105
24+
# s consists only of printable ASCII characters.
25+
26+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
27+
# CREATING A NEW STRING:
28+
29+
# create a new empty string
30+
# iterate over the current string- if a character is alphanumeric, lowercase it and it to the new string.
31+
# compare new string and new string in reverse; return answer
32+
33+
class Solution:
34+
def isPalindrome(self, s: str) -> bool:
35+
36+
newStr = ''
37+
38+
for c in s:
39+
if c.isalnum():
40+
newStr += c.lower()
41+
42+
return newStr == newStr[::-1]
43+
44+
Time Complexity = O(n)
45+
Space Complexity = O(n)
46+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
47+
48+
# TWO POINTERS:
49+
50+
# define a function to check if a character is alphanumeric using ord(c)
51+
# use two pointers approach- l and r
52+
# keep a check for l < r
53+
# keep check again for l < r to ensure the pointers stay within bounds during the search for alphanumeric characters.
54+
# this helps ensure that once the pointers meet or cross, the function can return without unnecessary checks.
55+
# if an alphanumeric character is not met, go ahead(l + 1, r -1 )
56+
# if an alphanumeric character is met, lowercase and compare l and r
57+
# if they are not equal, return False
58+
# if they are equal, loop continues
59+
# if loop does not return False and exits, return True
60+
61+
class Solution:
62+
def isPalindrome(self, s: str) -> bool:
63+
l, r = 0, len(s) - 1
64+
65+
while l < r:
66+
while l < r and not self.alphaNum(s[l]):
67+
l += 1
68+
while r > l and not self.alphaNum(s[r]):
69+
r -= 1
70+
if s[l].lower() != s[r].lower():
71+
return False
72+
l, r = l + 1, r - 1
73+
return True
74+
75+
def alphaNum(self, c):
76+
return (ord('A') <= ord(c) <= ord('Z') or
77+
ord('a') <= ord(c) <= ord('z') or
78+
ord('0') <= ord(c) <= ord('9'))
79+
80+
# can use Python in-built function isalnum()
81+
# return c.isalnum()
82+
83+
Time Complexity = O(n)
84+
Space Complexity = O(1)
85+
86+
# Companies:
87+
# Meta- 53
88+
# Google- 5
89+
# Amazon- 4
90+
# Apple- 2
91+
# Oracle- 2
92+
# Microsoft- 8
93+
# Bloomberg- 4
94+
# Uber- 3
95+
# TCS- 2
96+
# Goldman Sachs- 2
97+
# Zenefits- 2
98+
# Yandex- 45
99+
# Adobe- 20
100+
# Yahoo- 6
101+
# Toast- 6
102+
# EPAM Systems- 5
103+
# Spotify- 4
104+
# Tiktok- 3
105+
# American Express- 3
106+
# Accenture- 3
107+
# Deloitte- 2

0 commit comments

Comments
 (0)