-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitString.py
More file actions
56 lines (39 loc) · 1.16 KB
/
splitString.py
File metadata and controls
56 lines (39 loc) · 1.16 KB
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
def split_string(s):
"""
Return total number of ways to split the given string into three non-overlapping
substrings having the same number of As.
"""
len_of_string = len(s)
total_num_As = 0 #initialize sum of As
#compute the sum of zeroes
for i in range(len_of_string):
if s[i] == 'a':
total_num_As += 1
#1st edge case
#sum of As is not divisible by 3
#Return zero
if total_num_As % 3 != 0:
return 0
#2nd edge case
#sum of As is 0
#Return zero
if total_num_As == 0:
return 0
#initialize sum of As in each string part
sum_of_As_in_str_part = total_num_As // 3
#initialize ways to cut string
first_cut,second_cut = 0,0
#initialize count
a_count = 0
#loop from the start
for i in range(len_of_string):
#increment count if element is '0'
if s[i] == 'a':
a_count += 1
if (a_count == sum_of_As_in_str_part):
first_cut += 1
elif (a_count == 2 * sum_of_As_in_str_part):
second_cut += 1
return first_cut * second_cut
#driver code
print(split_string('ababa'))