forked from computiq/GIZ-pass-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-pass.py
More file actions
34 lines (21 loc) · 807 Bytes
/
Copy pathpython-pass.py
File metadata and controls
34 lines (21 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
@staticmethod
def longest_palindromic(s: str) -> str:
palindromic = ''
# loop through the input string
for i in range(len(s)):
# loop backwards through the input string
for j in range(len(s), i, -1):
# Break if out of range
if len(palindromic) >= j-i:
break
# Update variable if matches
elif s[i:j] == s[i:j][::-1]:
palindromic = s[i:j]
break
return palindromic
# test command:
print(Solution.longest_palindromic('babad')) # >> bab
print(Solution.longest_palindromic('cbbd')) # >> bb
print(Solution.longest_palindromic('a')) # >> a
print(Solution.longest_palindromic('ac')) # >> a