File tree 2 files changed +52
-11
lines changed
2 files changed +52
-11
lines changed Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change 82
82
{
83
83
"metadata" : {
84
84
"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 "
87
87
}
88
88
},
89
89
"cell_type" : " code" ,
101
101
]
102
102
}
103
103
],
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
113
105
}
114
106
],
115
107
"metadata" : {
You can’t perform that action at this time.
0 commit comments