Count ABC Strings
MediumPython 3Dynamic programmingRecursion
Given a length n, return how many strings of length n can be formed from the letters 'a', 'b' and 'c' subject to two rules: - the string contains at most one 'b', and - it never contains three or more consecutive 'c' (at most two in a row).
Constraints: - n >= 0. The empty string (n = 0) counts as 1 valid string.
Examples: n = 1 -> 3 # a, b, c n = 2 -> 8 # all 9 pairs except "bb" n = 3 -> 19 # e.g. aaa, aab, aba, abc, cca, ccb, ... (excludes bb*, ccc) n = 4 -> 43
Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…