Skip to content

Commit 2e6729c

Browse files
committed
Adding challenge 5.
1 parent e00257f commit 2e6729c

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# p_5_longest_palindromic_substring
2+
3+
🔗 https://leetcode.com/problems/longest-palindromic-substring/description/?source=submission-noac
4+
5+
## Leetcode - 5 - Longest palindromic substring
6+
7+
Given a string s, return the longest palindromic substring in s.
8+
9+
Example 1:
10+
11+
Input: s = "babad"
12+
Output: "bab"
13+
Explanation: "aba" is also a valid answer.
14+
15+
Example 2:
16+
17+
Input: s = "cbbd"
18+
Output: "bb"
19+
20+
Constraints:
21+
22+
1 <= s.length <= 1000
23+
s consist of only digits and English letters.

p_5_longest_palindromic_substring/src/__init__.py

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://github.com/badprog/badprog_leetcode_python
2+
3+
################
4+
# import
5+
import sys
6+
import pathlib
7+
8+
# Adding the parent directory of this file to the sys.path
9+
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
10+
11+
from src.solution import Solution
12+
from typing import List
13+
14+
################
15+
#
16+
def main():
17+
print("Hello from Badprog, Leetcode challenge 5 and Rust :D")
18+
solution = Solution()
19+
s = "abracadabra"
20+
result = solution.longestPalindrome(s)
21+
print("result = {}", result)
22+
23+
################
24+
#
25+
if __name__ == "__main__":
26+
main()
27+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# https://github.com/badprog/badprog_leetcode_python
2+
3+
############################
4+
#
5+
class Solution:
6+
############################
7+
#
8+
def longestPalindrome(self, s: str) -> str:
9+
# 1
10+
special_char = '#'
11+
str_ready = special_char.join(s)
12+
str_ready = special_char + str_ready + special_char
13+
14+
# 2
15+
str_len = len(str_ready)
16+
vec_palindrom = [0] * str_len
17+
# print(f"str_len: {str_len}, vec_palindrom:{vec_palindrom}")
18+
palindrom_center = 0
19+
palindrom_right = 0
20+
21+
# 3
22+
for (index, element) in enumerate(str_ready):
23+
# print(f"index: {index}, element: {element}")
24+
if index < palindrom_right:
25+
vec_palindrom[index] = min(palindrom_right - index, vec_palindrom[2 * palindrom_center - index])
26+
else:
27+
vec_palindrom[index] = 0
28+
29+
# 4
30+
while (index + vec_palindrom[index] + 1 < str_len and
31+
index - vec_palindrom[index] -1 >= 0 and
32+
str_ready[index - vec_palindrom[index] - 1] ==
33+
str_ready[index + vec_palindrom[index] + 1]):
34+
vec_palindrom[index] += 1
35+
36+
# 5
37+
if index + vec_palindrom[index] > palindrom_right:
38+
palindrom_center = index
39+
palindrom_right = index + vec_palindrom[index]
40+
41+
# 6
42+
# max_center = max(int(char) for char in vec_palindrom if char.isdigit())
43+
max_center = max(vec_palindrom)
44+
max_index = vec_palindrom.index(max_center)
45+
# print(f"vec_palindrom: {vec_palindrom}")
46+
# print(f"max_center: {max_center}")
47+
# print(f"max_index: {max_index}")
48+
start = (max_index - max_center) // 2
49+
# print(f"start: {start}")
50+
end = start + max_center
51+
52+
str_to_return = s[start:end]
53+
# print(f"str_to_return: {str_to_return}")
54+
55+
# return
56+
return str_to_return
57+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://github.com/badprog/badprog_leetcode_python
2+
3+
############################
4+
# import
5+
import sys
6+
import pathlib
7+
8+
# Adding the parent directory of this file to the sys.path
9+
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
10+
11+
import pytest
12+
from src.solution import Solution
13+
14+
@pytest.mark.parametrize("input_str, expected", [
15+
("babad", {"bab", "aba"}),
16+
("cbbd", {"bb"}),
17+
("a", {"a"}),
18+
("racecar", {"racecar"}),
19+
("aaaa", {"aaaa"}),
20+
("abc", {"a", "b", "c"}),
21+
("", {""}),
22+
("ababa", {"ababa"}),
23+
("abcdedcba", {"abcdedcba"}),
24+
("abbcccbba", {"abbcccbba"}),
25+
])
26+
27+
def test_longest_palindrome(input_str, expected):
28+
solution = Solution()
29+
result = solution.longestPalindrome(input_str)
30+
assert result in expected, f"Failed for entry={input_str}, expected={expected}, current={result}"
31+

0 commit comments

Comments
 (0)