This commit is contained in:
parent
a89091c155
commit
51e5a1a04e
1 changed files with 20 additions and 0 deletions
20
medium/unique_length_3_palindromic_subsequences.py
Normal file
20
medium/unique_length_3_palindromic_subsequences.py
Normal 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)
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue