Statistics

Problem Statement for "ForbiddenStrings"

Problem Statement

A string of letters A, B, C is forbidden if there are three consecutive letters from which one is A, one is B, and one is C. For example, BAACAACCBAAA is forbidden, while AABBCCAABB is not.

Your task is to calculate how many such strings of length n are not forbidden.

Definition

Class:
ForbiddenStrings
Method:
countNotForbidden
Parameters:
int
Returns:
long
Method signature:
long countNotForbidden(int n)
(be sure your method is public)

Constraints

  • n will be between 1 and 30, inclusive.

Examples

  1. 2

    Returns: 9

    All 9 strings of length 2 are not forbidden.

  2. 3

    Returns: 21

    There are 27 strings of length 3. Of these, 6 contain one occurrence of each letter. Those 6 strings are forbidden, so you should return 21.

  3. 4

    Returns: 51

  4. 1

    Returns: 3

  5. 5

    Returns: 123

  6. 10

    Returns: 10089

  7. 20

    Returns: 67858611

  8. 30

    Returns: 456417007497

  9. 6

    Returns: 297

  10. 7

    Returns: 717

  11. 8

    Returns: 1731

  12. 9

    Returns: 4179

  13. 11

    Returns: 24357

  14. 12

    Returns: 58803

  15. 13

    Returns: 141963

  16. 14

    Returns: 342729

  17. 15

    Returns: 827421

  18. 16

    Returns: 1997571

  19. 17

    Returns: 4822563

  20. 18

    Returns: 11642697

  21. 19

    Returns: 28107957

  22. 21

    Returns: 163825179

  23. 22

    Returns: 395508969

  24. 23

    Returns: 954843117

  25. 24

    Returns: 2305195203

  26. 25

    Returns: 5565233523

  27. 26

    Returns: 13435662249

  28. 27

    Returns: 32436558021

  29. 28

    Returns: 78308778291

  30. 29

    Returns: 189054114603


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: