-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspecial_subsequences_AG.py
76 lines (70 loc) · 1.27 KB
/
special_subsequences_AG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -----------------------------------------------
# Author : Mohit Chaudhari
# Created Date : 12/07/21
# -----------------------------------------------
# ***** Special Subsequences "AG" *****
# Problem Description
#
# You have given a string A having Uppercase English letters.
#
# You have to find that how many times subsequence "AG" is there in the given string.
#
# NOTE: Return the answer modulo 109 + 7 as the answer can be very large.
#
#
#
# Problem Constraints
# 1 <= length(A) <= 105
#
#
#
# Input Format
# First and only argument is a string A.
#
#
#
# Output Format
# Return an integer denoting the answer.
#
#
#
# Example Input
# Input 1:
#
# A = "ABCGAG"
# Input 2:
#
# A = "GAB"
#
#
# Example Output
# Output 1:
#
# 3
# Output 2:
#
# 0
#
#
# Example Explanation
# Explanation 1:
#
# Subsequence "AG" is 3 times in given string
# Explanation 2:
#
# There is no subsequence "AG" in the given string.
class Solution:
# @param A : string
# @return an integer
def solve(self, A):
cntA = 0
cntG = 0
for i in reversed(A):
if i == "G":
cntG += 1
if i == "A":
cntA += cntG
return cntA
s = Solution()
print(s.solve("GUGPUAGAFQBMPYAGGAAOALAELGGGAOGLGEGZ"))
# OUTPUT: 52