Skip to content

Commit 1e97028

Browse files
125 done
1 parent c4f3587 commit 1e97028

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

125_Valid Palindrome/solution.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
###A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all
2+
# non-alphanumeric characters, 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+
#
7+
#
8+
# Example 1:
9+
#
10+
# Input: s = "A man, a plan, a canal: Panama"
11+
# Output: true
12+
# Explanation: "amanaplanacanalpanama" is a palindrome.
13+
# Example 2:
14+
#
15+
# Input: s = "race a car"
16+
# Output: false
17+
# Explanation: "raceacar" is not a palindrome.
18+
# Example 3:
19+
#
20+
# Input: s = " "
21+
# Output: true
22+
# Explanation: s is an empty string "" after removing non-alphanumeric characters.
23+
# Since an empty string reads the same forward and backward, it is a palindrome.
24+
#
25+
26+
27+
s = "A man, a plan, a canal: Panama"
28+
29+
def palindrome(s):
30+
string = ''
31+
32+
for i in s:
33+
if i.isalpha() or i.isnumeric():
34+
string += i.lower()
35+
36+
37+
left, right = 0, len(string) - 1
38+
39+
while left < right:
40+
if string[left] == string[right]:
41+
left += 1
42+
right -= 1
43+
else:
44+
return False
45+
return True
46+
47+
48+
49+
print(palindrome(s))

practice_small_code.ipynb

+3-11
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282
{
8383
"metadata": {
8484
"ExecuteTime": {
85-
"end_time": "2024-08-07T17:27:28.052977Z",
86-
"start_time": "2024-08-07T17:27:28.048411Z"
85+
"end_time": "2024-08-08T01:55:13.160751Z",
86+
"start_time": "2024-08-08T01:55:13.152733Z"
8787
}
8888
},
8989
"cell_type": "code",
@@ -101,15 +101,7 @@
101101
]
102102
}
103103
],
104-
"execution_count": 10
105-
},
106-
{
107-
"metadata": {},
108-
"cell_type": "code",
109-
"outputs": [],
110-
"execution_count": null,
111-
"source": "",
112-
"id": "3afd94d8f063bc04"
104+
"execution_count": 1
113105
}
114106
],
115107
"metadata": {

0 commit comments

Comments
 (0)