Top 150 Interview

Palindromic Substrings

Detailed guide and Python implementation for the 'Palindromic Substrings' problem.

問題提起

Given a string s, return the number of palindromic substrings in it.

A string is a palindrome when it reads the same backward as forward. A substring is a contiguous sequence of characters within the string.

Write a function countSubstrings(s: str) -> int.

制約
  • 1 <= len(s) <= 1000
  • s consists of lowercase English letters

Example 1
Input
s = "abc"
Output
3
Explanation

Three palindromic substrings: "a", "b", "c".

Example 2
Input
s = "aaa"
Output
6
Explanation

Six palindromic substrings: "a", "a", "a", "aa", "aa", "aaa".

Need a Hint?
Consider using 1D DP-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。