Problem of the day
You are given a positive integer 'N'. Your task is to find the number of strings of length ‘N’ that can be formed using only the characters ‘a’, ‘b’ and ‘c’. The strings formed should be such that the number of ‘b’ and ‘c’ in the string is at most 1 and 2, respectively.
Example:Let’s say N = 2. The strings of length 2, which satisfy the given constraints are: “aa”, “ab”, “ac”, “ba”, “bc”, “ca”, “cb”, “cc”. Hence, the output is 8.
The first line of input contains an integer ‘T’ representing the number of test cases.
The first and the only line of every test case contains a positive integer ‘N’ representing the length of strings to be found.
Output Format:
For each test case, the number of strings of length ‘N’ which satisfy the given constraints is printed.
Print the output of each test case in a separate line.
Note:
Since the number of possible strings can be very large, print the answer modulo 1000000007.
You do not need to print anything, it has already been taken care of. Just implement the given function.
Follow Up:
Can you solve the problem in constant time and using constant extra space i.e. O(1) time and space complexity?
1 <= T <= 100
1 <= N <= 3000
Where 'T' denotes the number of test cases, 'N' denotes the length of strings.
Time Limit: 1 sec
2
2
3
8
19
For the first test case, refer to the example explained before.
For the second test case, N = 3. The strings of length 3, which satisfy the given constraints are: “aaa”, “aab”, “aac”, “aba”, “abc”, “aca”, “acb”, “acc”, and so on. There are a total of 19 possible strings.
2
4
1
39
3
Can you solve it using recursion?
Algorithm:
O(3^N), where N is the given positive integer.
In the worst case, we are making three recursive calls to the function and the depth of the recursion tree is N. Hence the overall time complexity is O(3^N).
O(N), where N is the given positive integer.
In the worst case, O(N) extra space is required for the recursion stack. Hence, the overall space complexity is O(N).