Skip to content

Commit 58af9ae

Browse files
committed
[LeetCode Sync] Runtime - 115 ms (82.69%), Memory - 17.8 MB (50.34%)
1 parent bd763fc commit 58af9ae

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<p>Given a string <code>s</code>, return <em>the number of <strong>palindromic substrings</strong> in it</em>.</p>
2+
3+
<p>A string is a <strong>palindrome</strong> when it reads the same backward as forward.</p>
4+
5+
<p>A <strong>substring</strong> is a contiguous sequence of characters within the string.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> s = &quot;abc&quot;
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> Three palindromic strings: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;aaa&quot;
20+
<strong>Output:</strong> 6
21+
<strong>Explanation:</strong> Six palindromic strings: &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;aa&quot;, &quot;aa&quot;, &quot;aaa&quot;.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
29+
<li><code>s</code> consists of lowercase English letters.</li>
30+
</ul>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
result, n = 0, len(s)
4+
for k in range(n * 2 - 1):
5+
i, j = k // 2, (k + 1) // 2
6+
while ~i and j < n and s[i] == s[j]:
7+
result += 1
8+
i, j = i - 1, j + 1
9+
10+
return result

0 commit comments

Comments
 (0)