Skip to content

Commit d3bc6bd

Browse files
committed
Sync LeetCode submission Runtime - 490 ms (66.59%), Memory - 18.4 MB (18.68%)
1 parent 9d99df4 commit d3bc6bd

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>Given a string <code>s</code>, return <em>the number of <strong>unique palindromes of length three</strong> that are a <strong>subsequence</strong> of </em><code>s</code>.</p>
2+
3+
<p>Note that even if there are multiple ways to obtain the same subsequence, it is still only counted <strong>once</strong>.</p>
4+
5+
<p>A <strong>palindrome</strong> is a string that reads the same forwards and backwards.</p>
6+
7+
<p>A <strong>subsequence</strong> of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.</p>
8+
9+
<ul>
10+
<li>For example, <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;<u>a</u>b<u>c</u>d<u>e</u>&quot;</code>.</li>
11+
</ul>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;aabca&quot;
18+
<strong>Output:</strong> 3
19+
<strong>Explanation:</strong> The 3 palindromic subsequences of length 3 are:
20+
- &quot;aba&quot; (subsequence of &quot;<u>a</u>a<u>b</u>c<u>a</u>&quot;)
21+
- &quot;aaa&quot; (subsequence of &quot;<u>aa</u>bc<u>a</u>&quot;)
22+
- &quot;aca&quot; (subsequence of &quot;<u>a</u>ab<u>ca</u>&quot;)
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;adc&quot;
29+
<strong>Output:</strong> 0
30+
<strong>Explanation:</strong> There are no palindromic subsequences of length 3 in &quot;adc&quot;.
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> s = &quot;bbcbaba&quot;
37+
<strong>Output:</strong> 4
38+
<strong>Explanation:</strong> The 4 palindromic subsequences of length 3 are:
39+
- &quot;bbb&quot; (subsequence of &quot;<u>bb</u>c<u>b</u>aba&quot;)
40+
- &quot;bcb&quot; (subsequence of &quot;<u>b</u>b<u>cb</u>aba&quot;)
41+
- &quot;bab&quot; (subsequence of &quot;<u>b</u>bcb<u>ab</u>a&quot;)
42+
- &quot;aba&quot; (subsequence of &quot;bbcb<u>aba</u>&quot;)
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>3 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>s</code> consists of only lowercase English letters.</li>
51+
</ul>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Approach 1: Count Letters In-Between
2+
3+
# Time: O(n)
4+
# Space: O(1), letters and between cannot grow beyond a size of 26, since s only contains letters of the English alphabet.
5+
6+
class Solution:
7+
def countPalindromicSubsequence(self, s: str) -> int:
8+
letters = set(s)
9+
ans = 0
10+
11+
for letter in letters:
12+
i, j = s.index(letter), s.rindex(letter)
13+
between = set()
14+
15+
for k in range(i + 1, j):
16+
between.add(s[k])
17+
18+
ans += len(between)
19+
20+
return ans
21+

0 commit comments

Comments
 (0)