I. Description
II. Code
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
# 변수 초기화
result = 0
for char in set(s):
# char가 문자열에서 등장하는 처음과 마지막 인덱스 찾기
first = s.find(char)
last = s.rfind(char)
if last - first > 1: # 두 char 사이에 문자가 존재할 경우
unique_middle = set(s[first+1:last])
result += len(unique_middle)
return result
처음부터 출발하는 포인터 하나, 끝부터 출발하는 포인터 하나를 놓고 같은 문자를 찾은 후 그 사이에 중복하지 않은 알파벳 개수를 세는 과정을 반복하면 된다.
'Coding Test > Hash Table' 카테고리의 다른 글
[LeetCode] 17. Letter Combinations of a Phone Number (0) | 2025.01.25 |
---|---|
[LeetCode] 12. Integer to Roman (0) | 2025.01.25 |
[LeetCode] 1943. Describe the Painting (0) | 2025.01.23 |
[LeetCode] 1942. The Number of the Smallest Unoccupied Chair (0) | 2025.01.22 |
[LeetCode] 3. Longest Substring Without Repeating Characters (0) | 2024.12.05 |