As a part of its competition, the school will conduct a codeathon, Lock the Code, where it has been given a value, and the participants have to decode it.
The participants are given a value denoting the number of rows in the matrix; they need to print the pattern.
Example :
For N=5, Pattern:
E
DE
CDE
BCDE
ABCDE
Among the participants, Ninja is new to programming and doesn’t have much experience; he asks you to solve the problem. Can you help solve this problem?
The first line of input contains an integer ‘T,’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ , denoting the number of rows in the matrix.
Output Format:
For each test case, print 'N' strings denoting every row in a new line (row elements not separated by space)
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 26
Time Limit: 1 sec
2
5
4
E
DE
CDE
BCDE
ABCDE
D
CD
BCD
ABCD
In the first test case, value of ‘N’ is 5, so print the ‘N’ rows from 1 to ‘N’ where in each row start from (N - i - 1)the character which goes on till ‘Nth character. Hence the answer is [‘E’,’DE’,’CDE,’ BCDE’,’ABCDE’].
In the second test case, the value of ‘N’ is 4, so print the ‘N’ rows from 1 to ‘N’ where each row starts from (N - i - 1)the character, which goes on till ‘Nth character. Hence the answer is [‘D’,’CD’,BCD’,’ABCD’].
2
3
2
C
BC
ABC
B
AB
Check if you can manipulate the nested loops to print the result?
The idea is to use nested loops in such a way that yields the answer.
The steps are as follows:
O(N^2), where N is the number of rows in the matrix.
We are traversing each row of the matrix that takes O(N) time complexity, and inside each loop, we are using an additional loop between the columns, which takes extra time (N). Hence the overall time complexity is O(N^2).
O(N), where N is the number of rows in the matrix.
We are using a temporary vector to store characters until max N. Hence, the overall space complexity is O(N).