diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_140_longest_palindromic_substring.py b/src/my_project/interviews/top_150_questions_round_22/ex_140_longest_palindromic_substring.py new file mode 100644 index 00000000..d8a2ff06 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_140_longest_palindromic_substring.py @@ -0,0 +1,19 @@ +class Solution: + def longestPalindrome(self, s: str) -> str: + start, max_len = 0, 1 + + def expand(left: int, right: int) -> None: + nonlocal start, max_len + while left >= 0 and right < len(s) and s[left] == s[right]: + left -= 1 + right += 1 + length = right - left - 1 + if length > max_len: + max_len = length + start = left + 1 + + for i in range(len(s)): + expand(i, i) # odd-length + expand(i, i + 1) # even-length + + return s[start:start + max_len] diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_140_longest_palindromic_substring.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_140_longest_palindromic_substring.ts new file mode 100644 index 00000000..9f6b5997 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_140_longest_palindromic_substring.ts @@ -0,0 +1,22 @@ +function longestPalindrome(s: string): string { + let start = 0, maxLen = 1; + + const expand = (left: number, right: number): void => { + while (left >= 0 && right < s.length && s[left] === s[right]) { + left--; + right++; + } + const length = right - left - 1; + if (length > maxLen) { + maxLen = length; + start = left + 1; + } + }; + + for (let i = 0; i < s.length; i++) { + expand(i, i); // odd-length + expand(i, i + 1); // even-length + } + + return s.substring(start, start + maxLen); +} diff --git a/tests/test_150_questions_round_22/test_140_longest_palindromic_substring_round_22.py b/tests/test_150_questions_round_22/test_140_longest_palindromic_substring_round_22.py new file mode 100644 index 00000000..f643e668 --- /dev/null +++ b/tests/test_150_questions_round_22/test_140_longest_palindromic_substring_round_22.py @@ -0,0 +1,35 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_140_longest_palindromic_substring import Solution + + +class LongestPalindromicSubstringTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + result = self.solution.longestPalindrome("babad") + self.assertIn(result, ("bab", "aba")) + + def test_example_2(self): + self.assertEqual("bb", self.solution.longestPalindrome("cbbd")) + + def test_single_char(self): + self.assertEqual("a", self.solution.longestPalindrome("a")) + + def test_all_same(self): + self.assertEqual("aaaa", self.solution.longestPalindrome("aaaa")) + + def test_no_palindrome_longer_than_one(self): + result = self.solution.longestPalindrome("abcd") + self.assertEqual(1, len(result)) + + def test_even_palindrome(self): + self.assertEqual("abba", self.solution.longestPalindrome("cabba")) + + def test_full_string_palindrome(self): + self.assertEqual("racecar", self.solution.longestPalindrome("racecar")) + + +if __name__ == '__main__': + unittest.main()