mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
52 lines
2.0 KiB
HTML
52 lines
2.0 KiB
HTML
<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>
|
|
|
|
<p>Note that even if there are multiple ways to obtain the same subsequence, it is still only counted <strong>once</strong>.</p>
|
|
|
|
<p>A <strong>palindrome</strong> is a string that reads the same forwards and backwards.</p>
|
|
|
|
<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>
|
|
|
|
<ul>
|
|
<li>For example, <code>"ace"</code> is a subsequence of <code>"<u>a</u>b<u>c</u>d<u>e</u>"</code>.</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "aabca"
|
|
<strong>Output:</strong> 3
|
|
<strong>Explanation:</strong> The 3 palindromic subsequences of length 3 are:
|
|
- "aba" (subsequence of "<u>a</u>a<u>b</u>c<u>a</u>")
|
|
- "aaa" (subsequence of "<u>aa</u>bc<u>a</u>")
|
|
- "aca" (subsequence of "<u>a</u>ab<u>ca</u>")
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "adc"
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong> There are no palindromic subsequences of length 3 in "adc".
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "bbcbaba"
|
|
<strong>Output:</strong> 4
|
|
<strong>Explanation:</strong> The 4 palindromic subsequences of length 3 are:
|
|
- "bbb" (subsequence of "<u>bb</u>c<u>b</u>aba")
|
|
- "bcb" (subsequence of "<u>b</u>b<u>cb</u>aba")
|
|
- "bab" (subsequence of "<u>b</u>bcb<u>ab</u>a")
|
|
- "aba" (subsequence of "bbcb<u>aba</u>")
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>3 <= s.length <= 10<sup>5</sup></code></li>
|
|
<li><code>s</code> consists of only lowercase English letters.</li>
|
|
</ul>
|