diskusi thread
melihat @adityadedhia/LeetCode 5 - Longest Palindroโฆ
1ย minggu, 4ย hari yang lalu - macroblocks
ยท 0
menit baca
Topics: ๐ป cs๐ท๏ธ problem๐ leetcode๐ medium
LeetCode Link: longest palindromic substringwriteup
Instead of checking if every substring is a palindrome (which requires checking each character), we can observe that palindromes expand symmetrically from their center. For any palindrome, if we start from the middle and expand outward, the characters on both sides will match.
1.ย For each possible center position in the string, expand outward while characters match
2. Track the longest palindrome found during all expansions
3.ย Handle both odd-length palindromes (single character center) and even-length palindromes (two character center) by expanding from each position twice
4. Return the substring corresponding to the longest palindrome discovered
ย ย Time complexity: O(n2) - we check 2n-1 centers, and each expansion can take up to O(n) time in the worst case (e.g., a string of all same characters)
ย ย Space complexity: O(1) - we only store indices and the result substring, no additional data structures needed
class Solution:
ย ย def longestPalindrome(self, s: str) โ str:
ย ย ย ย if len(s) < 2 or len(set(s)) == 1: # 1 or fewer is always a palindrome
ย ย ย ย ย ย return s
ย ย ย ย max_palindrome_len: int = 0
ย ย ย ย max_palindrome_substring: str = ""
ย ย ย ย len_s: int = len(s)
ย ย ย ย def is_palindrome(string_to_test: str) โ bool: # abc
ย ย ย ย ย ย len_string = len(string_to_test)
ย ย ย ย ย ย for i in range(len_string // 2):
ย ย ย ย ย ย ย ย if string_to_test[i] โ string_to_test[len_string - i - 1]:
ย ย ย ย ย ย ย ย ย ย return False
ย ย ย ย ย ย return True
ย ย ย ย for i in range(len_s):
ย ย ย ย ย ย starting_char_in_s: str = s[i]
ย ย ย ย ย ย for j in range(i, len_s):
ย ย ย ย ย ย ย ย substring_to_test: str = s[i:j+1]
ย ย ย ย ย ย ย ย substring_to_test_len = len(substring_to_test)
ย ย ย ย ย ย ย ย if substring_to_test_len > max_palindrome_len:
ย ย ย ย ย ย ย ย ย ย if is_palindrome(substring_to_test):
ย ย ย ย ย ย ย ย ย ย ย ย max_palindrome_len = substring_to_test_len
ย ย ย ย ย ย ย ย ย ย ย ย max_palindrome_substring = substring_to_test
ย ย ย ย return max_palindrome_substring
class Solution:
ย ย def longestPalindrome(self, s: str) โ str:
ย ย ย ย len_s: int = len(s)
ย ย ย ย if len_s < 2 or len(set(s)) == 1:
ย ย ย ย ย ย return s
ย ย ย ย def expand_palindrome(i: int) โ str:
ย ย ย ย ย ย previous_index: int = i - 1
ย ย ย ย ย ย next_index: int = i + 1
ย ย ย ย ย ย while previous_index โฅ 0 and next_index โค len_s - 1:
ย ย ย ย ย ย ย ย previous_char: str = s[previous_index]
ย ย ย ย ย ย ย ย next_char: str = s[next_index]
ย ย ย ย ย ย ย ย if previous_char == next_char:
ย ย ย ย ย ย ย ย ย ย previous_index -= 1
ย ย ย ย ย ย ย ย ย ย next_index += 1
ย ย ย ย ย ย ย ย else:
ย ย ย ย ย ย ย ย ย ย break
ย ย ย ย ย ย max_odd_palindrome: str = s[previous_index + 1:next_index]
ย ย ย ย ย ย current_index: int = i
ย ย ย ย ย ย next_index: int = i + 1
ย ย ย ย ย ย while current_index โฅ 0 and next_index โค len_s - 1:
ย ย ย ย ย ย ย ย current_char: str = s[current_index]
ย ย ย ย ย ย ย ย next_char: str = s[next_index]
ย ย ย ย ย ย ย ย if current_char == next_char:
ย ย ย ย ย ย ย ย ย ย current_index -= 1
ย ย ย ย ย ย ย ย ย ย next_index += 1
ย ย ย ย ย ย ย ย else:
ย ย ย ย ย ย ย ย ย ย break
ย ย ย ย ย ย max_even_palindrome: str = s[current_index + 1: next_index]
ย ย ย ย ย ย if len(max_odd_palindrome) > len(max_even_palindrome):
ย ย ย ย ย ย ย ย return max_odd_palindrome
ย ย ย ย ย ย return max_even_palindrome
ย ย ย ย max_palindrome_len: int = 0
ย ย ย ย max_palindrome_string: str = ""
ย ย ย ย for index_to_test in range(len_s): # 0(n)
ย ย ย ย ย ย max_palindrome_at_index: str = expand_palindrome(index_to_test) # O(n)
ย ย ย ย ย ย len_max_palindrome_at_index: int = len(max_palindrome_at_index)
ย ย ย ย ย ย if len_max_palindrome_at_index > max_palindrome_len:
ย ย ย ย ย ย ย ย max_palindrome_len = len_max_palindrome_at_index
ย ย ย ย ย ย ย ย max_palindrome_string = max_palindrome_at_index
ย ย ย ย return max_palindrome_string
0
0
19
nothing here
be the first to share your perspective


balas pos