aboutsummaryrefslogtreecommitdiff
path: root/unique_len_3_palindrome.py
blob: a055519b123c45629ce6560d7e3adf52d123e762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 1930. Unique Length-3 Palindromic Subsequences
import string

"""
given a string 's', return the number of unique palindromes of length three
that 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 thhe
remaining characters. for example: "ace" is a subsequence of "abcde"
"""


class Solution(object):
    def countPalindromicSubsequence(self, s):
        ans = 0
        for c in string.ascii_lowercase:
            i, j = s.find(c), s.rfind(c)
            if i > -1:
                ans += len(set(s[i + 1 : j]))
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.countPalindromicSubsequence("aabca"))  # expect: 3
    print(obj.countPalindromicSubsequence("adc"))  # expect: 0
    print(obj.countPalindromicSubsequence("bbcbaba"))  # expect: 4