Unique Length-3 Palindromic Subsequences
Real-world version: Three-Letter Mirrored PicksThe statement here is our own: the same inputs, outputs and technique.
A tape reads s, a string of lowercase letters. A pick takes three letters from it, keeping their order, and the pick is mirrored when its first and last letters are the same.
Return how many different mirrored picks the tape holds, counting two picks as the same when they read the same.
Examples
Example 1
- Input
- s = "bcabcaba"
- Output
- 8
Every one of a, b and c appears at least twice, and the distinct letters lying between the first and last place of each supply the middles, which comes to this many different picks.
Example 2
- Input
- s = "abc"
- Output
- 0
No letter appears twice, so no pick can have matching ends.
Example 3
- Input
- s = "aaa"
- Output
- 1
The only pick is "aaa".
Constraints
- 3 <= s.length <= 10^5
- s consists of lowercase English letters only
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def count_palindromic_subsequence(s: str) -> int:public int countPalindromicSubsequence(String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Number of Wonderful SubstringsReal-world version: Nearly Even Telemetry StretchesMedium
- Find the Longest Substring Containing Vowels in Even CountsReal-world version: The Longest Stretch With Every Tally EvenMedium
- Find the DifferenceReal-world version: The Sort That Joined the TrayEasy
- Longest Nice SubstringReal-world version: Longest Matched-Case RunEasy