Given a string s, return the number of unique palindromes of length three that are a subsequence of s.
Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
A palindrome is a string that reads the same forwards and backwards.
A subsequence 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.
For example, "ace" is a subsequence of "abcde".
subsequence : 상대적인 순서는 유지하면서 이어지지 않는 부분 배열
Ex. abcdef > ace 는 해당 문자열의 subsequence
Input: s = "aabca"
-aaa, aba, aca 총 3개의 회문이 있다.
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
placement = defaultdict(lambda : [-1, -1])
for i, ch in enumerate(s):
if placement[ch][0] == -1:
placement[ch][0] = i
else:
placement[ch][1] = i
ans = 0
for val in placement.values():
if val[0] == -1 or val[1] == -1:
continue
mid = s[val[0] + 1: val[-1]]
ans += len(set(mid))
return ans