Skip to content

Commit 3aab13b

Browse files
authored
Create maximize-number-of-subsequences-in-a-string.py
1 parent de72acf commit 3aab13b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# counting, greedy
5+
class Solution(object):
6+
def maximumSubsequenceCount(self, text, pattern):
7+
"""
8+
:type text: str
9+
:type pattern: str
10+
:rtype: int
11+
"""
12+
result = cnt1 = cnt2 = 0
13+
for c in text:
14+
if c == pattern[1]:
15+
result += cnt1
16+
cnt2 += 1
17+
if c == pattern[0]:
18+
cnt1 += 1
19+
return result + max(cnt1, cnt2) # add pattern[1] at back or pattern[0] at front

0 commit comments

Comments
 (0)