Skip to content

Commit 238766e

Browse files
authored
Create unique-length-3-palindromic-subsequences.py
1 parent c92fbb7 commit 238766e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def countPalindromicSubsequence(self, s):
6+
"""
7+
:type s: str
8+
:rtype: int
9+
"""
10+
first, last = [len(s)]*26, [-1]*26
11+
for i, c in enumerate(s):
12+
first[ord(c)-ord('a')] = min(first[ord(c)-ord('a')], i)
13+
last[ord(c)-ord('a')] = max(last[ord(c)-ord('a')], i)
14+
return sum(len(set(s[i] for i in xrange(first[c]+1, last[c]))) for c in xrange(26))

0 commit comments

Comments
 (0)