Skip to content

Commit 33d0d79

Browse files
committed
feat: brute force 알고리즘으로 1차 완료
1 parent b00545f commit 33d0d79

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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}"

0 commit comments

Comments
 (0)