java.lang.Object
g1901_2000.s1930_unique_length_3_palindromic_subsequences.Solution

public class Solution extends Object
1930 - Unique Length-3 Palindromic Subsequences.<p>Medium</p> <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>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;aabca&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The 3 palindromic subsequences of length 3 are:</p> <ul> <li> <p>&ldquo;aba&rdquo; (subsequence of &ldquo;aabca&rdquo;)</p> </li> <li> <p>&ldquo;aaa&rdquo; (subsequence of &ldquo;aabca&rdquo;)</p> </li> <li> <p>&ldquo;aca&rdquo; (subsequence of &ldquo;aabca&rdquo;)</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;adc&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There are no palindromic subsequences of length 3 in &ldquo;adc&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;bbcbaba&rdquo;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The 4 palindromic subsequences of length 3 are:</p> <ul> <li> <p>&ldquo;bbb&rdquo; (subsequence of &ldquo;bbcbaba&rdquo;)</p> </li> <li> <p>&ldquo;bcb&rdquo; (subsequence of &ldquo;bbcbaba&rdquo;)</p> </li> <li> <p>&ldquo;bab&rdquo; (subsequence of &ldquo;bbcbaba&rdquo;)</p> </li> <li> <p>&ldquo;aba&rdquo; (subsequence of &ldquo;bbcbaba&rdquo;)</p> </li> </ul> <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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countPalindromicSubsequence

      public int countPalindromicSubsequence(String s)