File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
longest-palindromic-substring Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ 시간 복잡도: O(n^2)
3+ 공간 복잡도: O(1)
4+ '''
5+
6+ class Solution :
7+ def longestPalindrome (self , s : str ) -> str :
8+ start , max_length = 0 , 1 # Track longest palindrome
9+
10+ def expand_around_center (left : int , right : int ) -> int :
11+ while left >= 0 and right < len (s ) and s [left ] == s [right ]:
12+ left -= 1
13+ right += 1
14+ # Return length of palindrome
15+ return right - left - 1
16+
17+ # Check each position as potential center
18+ for i in range (len (s )):
19+ # Check for odd length palindromes (single character center)
20+ len1 = expand_around_center (i , i )
21+ # Check for even length palindromes (between two characters)
22+ len2 = expand_around_center (i , i + 1 )
23+
24+ curr_max = max (len1 , len2 )
25+
26+ # Update start and max_length if current palindrome is longer
27+ if curr_max > max_length :
28+ max_length = curr_max
29+ start = i - (curr_max - 1 ) // 2
30+
31+ return s [start :start + max_length ]
You can’t perform that action at this time.
0 commit comments