bumpsoo 2025-01-04 12:40:12 +09:00
parent a89091c155
commit 51e5a1a04e

View file

@ -0,0 +1,20 @@
# https://leetcode.com/problems/unique-length-3-palindromic-subsequences
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
first, last = {}, {}
for i in range(len(s)):
if s[i] not in first:
first[s[i]] = i
last[s[i]] = i
palinedromes = set()
for ch in first:
i = first[ch]
j = last[ch]
if i + 1 < j:
middles = s[i + 1 : j]
for middle in middles:
palinedromes.add(''.join([ch, middle, ch]))
return len(palinedromes)