Skip to content

Instantly share code, notes, and snippets.

@grevych
Created September 17, 2020 01:24
Show Gist options
  • Save grevych/585723e3237499fd01bbd3a7ce1e7456 to your computer and use it in GitHub Desktop.
Save grevych/585723e3237499fd01bbd3a7ce1e7456 to your computer and use it in GitHub Desktop.
Find the number of possible combinations for groups of 3 substrings with the same amount of letter `a`
from collections import Counter
def solution_aux(S, groupSize, groups, numberOfAs):
print(groups, S, numberOfAs)
if len(groups) == 3:
if len(S) == 0:
print('count')
return 1
if len(groups) > 3:
return 0
if numberOfAs > groupSize:
return 0
if len(S) == 0:
return 0
result = 0
if S[0] == 'a':
numberOfAs += 1
if numberOfAs < groupSize:
groups[-1] += S[0]
result += solution_aux(S[1:], groupSize, groups[:], numberOfAs)
else:
groups[-1] += S[0]
result += solution_aux(S[1:], groupSize, groups[:], numberOfAs)
if numberOfAs > groupSize:
groups[-1] = groups[-1][:-1]
groups.append(S[0])
result += solution_aux(S[1:], groupSize, groups[:], 1)
else:
if numberOfAs < groupSize:
groups[-1] += S[0]
result += solution_aux(S[1:], groupSize, groups[:], numberOfAs)
groups[-1] = groups[-1][:-1]
groups.append(S[0])
result += solution_aux(S[1:], groupSize, groups[:], numberOfAs)
else:
groups[-1] += S[0]
result += solution_aux(S[1:], groupSize, groups[:], numberOfAs)
groups[-1] = groups[-1][:-1]
groups.append(S[0])
result += solution_aux(S[1:], groupSize, groups[:], 0)
return result
def solution(S):
letters = Counter(S)
if letters['a'] % 3 != 0:
return 0
groupSize = letters['a'] // 3
return solution_aux(S[1:], groupSize, [S[0]], int(S[0] == 'a'))
x = solution('bbbbb')
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment