Created
September 17, 2020 01:24
-
-
Save grevych/585723e3237499fd01bbd3a7ce1e7456 to your computer and use it in GitHub Desktop.
Revisions
-
grevych created this gist
Sep 17, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ 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)