---
# System prepended metadata

title: '#5 Longest Palindromic Substring'
tags: [LeetCode, ' Top100']

---

---
title: "#5 Longest Palindromic Substring"
tags: LeetCode, Top100
---
#5 Longest Palindromic Substring
==

題目描述
--
Given a string s, return the longest palindromic substring in s.

Example 1:
--
>Input: s = "babad"
>Output: "bab"
>Note: "aba" is also a valid answer.

<!--more-->

## Expand Around Center

```python
class Solution:
    def bruteforce(self, s: str) -> str:
        newStr = "#".join(f"^{s}$")
        sCenter, sRange = 0, 0
        print(newStr)
        for index in range(1, len(newStr)-1):
            times = 1
            strcenter, strRange = 0, 0
            while (newStr[index-times] == newStr[index+times]):
                times += 1
                strcenter = index
                strRange += 1
                if(strRange > sRange):
                    sRange = strRange
                    sCenter = strcenter

        if newStr[sCenter] != "#":
            addOne = 1
        else:
            addOne = 0
        fRange = sRange//2
        center = int(sCenter/2)-addOne
        return s[center-fRange:center+fRange+addOne]


if __name__ == "__main__":
    s = "cbba"
    print(Solution().bruteforce(s))
```
