Ninja likes to play with strings, and he calls a string ‘S’ Happy if it only contains letters ‘a’, ‘b’, and ‘c’, and no three consecutive letters in the string are the same. For example, ‘aa’, ‘aab’, ‘aabbcc’ are the happy strings, but ‘aaa’, ‘aaza’, ‘aaabbb’ are not happy strings.
You are given three non-negative integers ‘X’, ‘Y’, ‘Z’. You need to find the longest happy string such that it contains ‘a’ at most ‘X’ times, ‘b’ at most ‘Y’ times, and ‘c’ almost ‘Z’ times.
Note:
There can be more than one possible string with maximum size. In that case, you can return any of them.
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains three space separated integers, ‘X’, ‘Y’, and ‘Z’, which denote the maximum number of ‘a’, ‘b’, and ‘c’ respectively answer strings can have.
Output Format:
For each test case, the output will be “1” if you have returned the correct answer, else it will be “0”.
Note :
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
0 <= X, Y, Z <= 10^3
X + Y + Z >= 1
Time Limit: 1 sec
2
2 4 4
6 1 1
1
1
Test case 1:
For the first test case one of the possible string is “aabbccbbcc”, other possible string can be “abbccbbcca” or “bbcabbcacc”. All these strings have maximum possible length equal to 10, and no three consecutive letters are the same.
Test case 2:
For the second test case, one possible answer string is “aabaacaa” of length 8, another possible string is “aacaabaa”.
1
6 0 1
1
Test case 1:
There is only one possible string for the first test case i.e. “aacaa” of length 5.
Check all possible strings
Given 'X', 'Y' 'Z' you can try all combinations of possible strings and return the string that has a maximum length. You can create all possible strings by inserting all the three letters i.e. ‘a’, ‘b’, ‘c’ at each ‘i -th’ position in string by taking care of the condition that no three characters should be the same. For example at ‘i -th’ position in the string you can add the letter ‘a’, if and only if 'X' greater than 0 and the last two letters in string i.e. at position ‘i - 1’ and ‘i - 2’ are not ‘a’. Similarly of ‘b’ and ‘c’. At last check all the possible strings you generated and return the string with the maximum size.
The function will take two parameters:
bool check( 'STR', ‘c’ ):
The function will take four parameters:
void findLongest ( ‘X’, ‘Y’, ‘Z’, 'STR' ):
O(3 ^ (X + Y + Z)), where 'X', ‘Y’, ‘Z’ are the given maximum number ‘a’, ‘b’, and ‘c’ respectively that the output string can have.
As we are checking every possible string upto total length which is ‘(X + Y +Z)’, and at every ‘i-th’ position there can be 3 characters possible i.e ‘a’, ‘b’ and ‘c’ so the total strings possible in worst case are 3 ^ (X + Y + Z). Hence the over time complexity is O (3 ^ (X + Y + Z)).
O(X + Y + Z), where ‘X’, ‘Y’, ‘Z’ are the given maximum number ‘a’, ‘b’, and ‘c’ respectively output string can have.
In the worst case, we need to store a string of size ‘X + Y +Z’, that takes O (X + Y + Z) space. Hence the space complexity will be O (X + Y +Z).