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+ Constraints:
3+ - 1 <= s.length <= 1000
4+ - s consist of only digits and English letters.
5+
6+ Time Complexity: O(n^3)
7+ - ๋ชจ๋ ๋ถ๋ถ ๋ฌธ์์ด์ ๊ตฌํ ๋ O(n^2)
8+ - ๊ฐ ๋ถ๋ถ ๋ฌธ์์ด์ด ํฐ๋ฆฐ๋๋กฌ์ธ์ง๋ฅผ ์์๋ผ ๋ O(n)
9+
10+ Space Complexity: O(1)
11+
12+ Note:
13+ - ๋ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ ์๊ฐํด๋ณด๊ธฐ/์ฐพ์๋ณด๊ธฐ
14+ """
15+ # Solution 1: Brute force
16+ # ๋ฌธ์์ด์ ์์๊ฐ๊ณผ ๋๊ฐ์ ์ด์ฉํ์ฌ ๊ฐ์ฅ ๊ธด ํฐ๋ฆฐ๋๋กฌ์ผ๋ก ์
๋ฐ์ดํธํ๋ ๋ฐฉ์
17+ class Solution :
18+ def longestPalindrome (self , s : str ) -> str :
19+ longest_palindrome = ""
20+ max_len = 0
21+
22+ for i in range (len (s )):
23+ for j in range (i , len (s )):
24+ substr = s [i :j + 1 ]
25+
26+ if substr == substr [::- 1 ]:
27+ if len (substr ) > max_len :
28+ max_len = len (substr )
29+ longest_palindrome = substr
30+
31+ return longest_palindrome
You canโt perform that action at this time.
0 commit comments