Nav: << previous: 4.寻找两个正序数组的中位数 | next: 6.Z 字形变换 >>
Description
tab: English
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "babad"
<strong>Output:</strong> "bab"
<strong>Explanation:</strong> "aba" is also a valid answer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> "bb"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consist of only digits and English letters.</li>
</ul>
> [!tip]- Hint 1
>
> How can we reuse a previously computed palindrome to compute a larger palindrome?
> [!tip]- Hint 2
>
> If “aba” is a palindrome, is “xabax” a palindrome? Similarly is “xabay” a palindrome?
> [!tip]- Hint 3
>
> Complexity based hint:</br>
If we use brute-force and check whether for every start and end position a substring is a palindrome we have O(n^2) start - end pairs and O(n) palindromic checks. Can we reduce the time for palindromic checks to O(1) by reusing some previous computation.
---
[submissions](https://leetcode.com/problems/longest-palindromic-substring/submissions/) | [solutions](https://leetcode.com/problems/longest-palindromic-substring/solutions/)
tab: 中文
<p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的 <span data-keyword="palindromic-string">回文</span> <span data-keyword="substring-nonempty">子串</span>。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>s = "babad"
<strong>输出:</strong>"bab"
<strong>解释:</strong>"aba" 同样是符合题意的答案。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>s = "cbbd"
<strong>输出:</strong>"bb"
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> 仅由数字和英文字母组成</li>
</ul>
> [!tip]- 提示 1
>
> How can we reuse a previously computed palindrome to compute a larger palindrome?
> [!tip]- 提示 2
>
> If “aba” is a palindrome, is “xabax” a palindrome? Similarly is “xabay” a palindrome?
> [!tip]- 提示 3
>
> Complexity based hint:</br>
If we use brute-force and check whether for every start and end position a substring is a palindrome we have O(n^2) start - end pairs and O(n) palindromic checks. Can we reduce the time for palindromic checks to O(1) by reusing some previous computation.
---
[提交记录](https://leetcode.cn/problems/longest-palindromic-substring/submissions/) | [题解](https://leetcode.cn/problems/longest-palindromic-substring/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122