class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = set()
def rec(i, cur):
if len(cur) == 3:
if cur[0] == cur[2]:
res.add(cur)
return
if i == len(s):
return
rec(i + 1, cur)
rec(i + 1, cur + s[i])
rec(0, "")
return len(res)Where is the length of the string and is the number of unique three length pallindromic subsequences (26 * 26 = 676).
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = set()
for i in range(len(s) - 2):
for j in range(i + 1, len(s) - 1):
for k in range(j + 1, len(s)):
if s[i] != s[k]:
continue
res.add(s[i] + s[j] + s[k])
return len(res)Where is the length of the string and is the number of unique three length pallindromic subsequences (26 * 26 = 676).
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = 0
for ends in range(ord('a'), ord('z') + 1):
for mid in range(ord('a'), ord('z') + 1):
seq = chr(ends) + chr(mid) + chr(ends)
idx, found = 0, 0
for c in s:
if seq[idx] == c:
idx += 1
if idx == 3:
found = 1
break
res += found
return resWhere is the length of the string and is the number of unique three length pallindromic subsequences (26 * 26 = 676).
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = set()
left = set()
right = collections.Counter(s)
for i in range(len(s)):
right[s[i]] -= 1
if right[s[i]] == 0:
right.pop(s[i])
for j in range(26):
c = chr(ord('a') + j)
if c in left and c in right:
res.add((s[i], c))
left.add(s[i])
return len(res)Where is the length of the string and is the number of unique three length pallindromic subsequences (26 * 26 = 676).
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
n = len(s)
prefix = [[0] * 26 for _ in range(n + 1)]
firstIndex = [-1] * 26
lastIndex = [-1] * 26
for i in range(n):
j = ord(s[i]) - ord('a')
if firstIndex[j] == -1:
firstIndex[j] = i
lastIndex[j] = i
prefix[i + 1] = prefix[i][:]
prefix[i + 1][j] += 1
res = 0
for ends in range(26):
if firstIndex[ends] == -1 or firstIndex[ends] == lastIndex[ends]:
continue
l, r = firstIndex[ends], lastIndex[ends]
for mid in range(26):
if prefix[r][mid] - prefix[l + 1][mid] > 0:
res += 1
return resclass Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = 0
for i in range(26):
c = chr(ord('a') + i)
l, r = s.find(c), s.rfind(c)
if l == -1 or l == r:
continue
mids = set()
for j in range(l + 1, r):
mids.add(s[j])
res += len(mids)
return resclass Solution:
def countPalindromicSubsequence(self, s: str) -> int:
firstIndex = [-1] * 26
lastIndex = [-1] * 26
for i in range(len(s)):
j = ord(s[i]) - ord('a')
if firstIndex[j] == -1:
firstIndex[j] = i
lastIndex[j] = i
res = 0
for ends in range(26):
if firstIndex[ends] == -1 or firstIndex[ends] == lastIndex[ends]:
continue
l, r = firstIndex[ends], lastIndex[ends]
mask = 0
for i in range(l + 1, r):
c = ord(s[i]) - ord('a')
if mask & (1 << c):
continue
mask |= (1 << c)
res += 1
return res