File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ # Palindromic Substrings (Medium)
2+ # Link - https://leetcode.com/problems/longest-palindromic-substring/
3+
4+ # Given a string s, return the number of palindromic substrings in it.
5+
6+ # A string is a palindrome when it reads the same backward as forward.
7+
8+ # A substring is a contiguous sequence of characters within the string.
9+
10+ # Example 1:
11+ # Input: s = "abc"
12+ # Output: 3
13+ # Explanation: Three palindromic strings: "a", "b", "c".
14+
15+
16+ class Solution :
17+ def countSubstrings (self , s : str ) -> int :
18+ res = 0
19+
20+ def count_palindrome (l , r ):
21+ count = 0
22+ while l >= 0 and r < len (s ) and s [l ] == s [r ]:
23+ count += 1
24+ l -= 1
25+ r += 1
26+ return count
27+
28+ for i in range (len (s )):
29+ res += count_palindrome (i , i )
30+ res += count_palindrome (i , i + 1 )
31+
32+ return res
You can’t perform that action at this time.
0 commit comments