Skip to content

Commit 8004c6e

Browse files
committed
Longest palindromic string
1 parent 066ad17 commit 8004c6e

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### Longest Palindromic Substring
2+
### Given a string s, return the longest palindromic substring therein
3+
4+
5+
fn longest_palindrome(s: String) -> String:
6+
if len(s) == 0:
7+
return s
8+
longest = String("")
9+
for i in range(len(s)):
10+
left, right = i, i
11+
find_longest(left, right, s, longest)
12+
left, right = i, i + 1
13+
find_longest(left, right, s, longest)
14+
return longest
15+
16+
17+
fn find_longest(mut left: Int, mut right: Int, s: String, mut longest: String):
18+
while 0 <= left and right < len(s) and s[left] == s[right]:
19+
if right - left + 1 > len(longest):
20+
longest = s[left : right + 1]
21+
left -= 1
22+
right += 1
23+
24+
25+
from testing import assert_true
26+
27+
28+
fn main() raises:
29+
var s: String = "babad"
30+
var expected: String = "bab"
31+
result = longest_palindrome(s)
32+
assert_true(result == expected, "Assertion failed")
33+
34+
s = "cbbd"
35+
expected = "bb"
36+
result = longest_palindrome(s)
37+
assert_true(result == expected, "Assertion failed")
38+
39+
s = "racecar"
40+
expected = "racecar"
41+
result = longest_palindrome(s)
42+
assert_true(result == expected, "Assertion failed")

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@
5252
🟢 [Longest Common Subsequence Recursive](longest_common_subeq.md) ➔ Return the length of the longest common subsequence between two strings, or 0 if none exists
5353

5454
🟢 [Longest Common Subsequence Dynamic](longest_common_subeq_dyn.md) ➔ Return the length of the longest common subsequence between two strings, or 0 if none exists
55+
56+
🟢 [Longest Palindromic Substring](longest_palidromic_substr.md) ➔ Given a string s, return the longest palindromic substring therein

docs/longest_palidromic_substr.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
### Longest Palindromic Substring
2+
### Given a string s, return the longest palindromic substring therein
3+
4+
```python
5+
6+
7+
fn longest_palindrome(s: String) -> String:
8+
if len(s) == 0:
9+
return s
10+
longest = String("")
11+
for i in range(len(s)):
12+
left, right = i, i
13+
find_longest(left, right, s, longest)
14+
left, right = i, i + 1
15+
find_longest(left, right, s, longest)
16+
return longest
17+
18+
19+
fn find_longest(mut left: Int, mut right: Int, s: String, mut longest: String):
20+
while 0 <= left and right < len(s) and s[left] == s[right]:
21+
if right - left + 1 > len(longest):
22+
longest = s[left : right + 1]
23+
left -= 1
24+
right += 1
25+
26+
27+
from testing import assert_true
28+
29+
30+
fn main() raises:
31+
var s: String = "babad"
32+
var expected: String = "bab"
33+
result = longest_palindrome(s)
34+
assert_true(result == expected, "Assertion failed")
35+
36+
s = "cbbd"
37+
expected = "bb"
38+
result = longest_palindrome(s)
39+
assert_true(result == expected, "Assertion failed")
40+
41+
s = "racecar"
42+
expected = "racecar"
43+
result = longest_palindrome(s)
44+
assert_true(result == expected, "Assertion failed")
45+
46+
```
47+
48+
49+
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/longest_palidromic_substr.mojo)

0 commit comments

Comments
 (0)