Advertisement
Iam_Sandeep

1930. Unique Length-3 Palindromic Subsequences

Jul 18th, 2022
1,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. '''
  2. Explanation
  3. For each palindromes in format of "aba",
  4. we enumerate the character on two side.
  5.  
  6. We find its first occurrence and its last occurrence,
  7. all the characters in the middle are the candidate for the midd char.
  8.  
  9.  
  10. Complexity
  11. Time O(26n)
  12. Space O(26n)
  13.  
  14.  
  15. '''
  16. class Solution:
  17.     def countPalindromicSubsequence(self, s: str) -> int:
  18.         ans=0
  19.         for i in range(26):
  20.             ch=chr(ord('a')+i)
  21.             l,r=s.find(ch),s.rfind(ch)
  22.             if r-l>1:
  23.                 ans+=len(set(s[l+1:r]))
  24.         return ans
  25.                
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement