|
| 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>"ace"</code> is a subsequence of <code>"<u>a</u>b<u>c</u>d<u>e</u>"</code>.</li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> s = "aabca" |
| 18 | +<strong>Output:</strong> 3 |
| 19 | +<strong>Explanation:</strong> The 3 palindromic subsequences of length 3 are: |
| 20 | +- "aba" (subsequence of "<u>a</u>a<u>b</u>c<u>a</u>") |
| 21 | +- "aaa" (subsequence of "<u>aa</u>bc<u>a</u>") |
| 22 | +- "aca" (subsequence of "<u>a</u>ab<u>ca</u>") |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> s = "adc" |
| 29 | +<strong>Output:</strong> 0 |
| 30 | +<strong>Explanation:</strong> There are no palindromic subsequences of length 3 in "adc". |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 3:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> s = "bbcbaba" |
| 37 | +<strong>Output:</strong> 4 |
| 38 | +<strong>Explanation:</strong> The 4 palindromic subsequences of length 3 are: |
| 39 | +- "bbb" (subsequence of "<u>bb</u>c<u>b</u>aba") |
| 40 | +- "bcb" (subsequence of "<u>b</u>b<u>cb</u>aba") |
| 41 | +- "bab" (subsequence of "<u>b</u>bcb<u>ab</u>a") |
| 42 | +- "aba" (subsequence of "bbcb<u>aba</u>") |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>3 <= s.length <= 10<sup>5</sup></code></li> |
| 50 | + <li><code>s</code> consists of only lowercase English letters.</li> |
| 51 | +</ul> |
0 commit comments