File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
longest-palindromic-substring Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestPalindrome (self , s : str ) -> str :
3+ answer = ""
4+
5+ for i in range (len (s )):
6+ for j in range (i , len (s )):
7+ subset = s [i : j + 1 ]
8+
9+ if subset == subset [::- 1 ]:
10+ if len (answer ) < len (subset ):
11+ answer = subset
12+ return answer
13+
14+
15+ if __name__ == "__main__" :
16+ test_cases = [
17+ ("babad" , ["bab" , "aba" ]),
18+ ("bb" , ["bb" ]),
19+ ("aaabaaa" , ["aaabaaa" ]),
20+ ("c" , ["c" ]),
21+ ("123a24542a0321" , ["a24542a" ]),
22+ ]
23+
24+ solution = Solution ()
25+ for idx , case_ in enumerate (test_cases ):
26+ s , answers = case_
27+ result = solution .longestPalindrome (s )
28+ assert (
29+ result in answers
30+ ), f"Test Case { idx } Failed: Expected one of { "," .join (answers )} , Got { result } "
You can’t perform that action at this time.
0 commit comments