Problem Solving/LeetCode
2023.11.14 Today's Challenge
fabichoi
2023. 11. 14. 23:45
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/
Unique Length-3 Palindromic Subsequences - LeetCode
Can you solve this real interview question? Unique Length-3 Palindromic Subsequences - 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 subse
leetcode.com
무식하게 구하면 TL이 나올 듯.
slice를 잘 해서 구현하는 방법이 있음
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
letters = set(s)
ans = 0
for letter in letters:
i, j = s.index(letter), s.rindex(letter)
between = set()
for k in range(i+1, j):
between.add(s[k])
ans += len(between)
return ans
반응형