Interesting Alphabets

Easy
0/40
Average time to solve is 10m
52 upvotes

Problem statement

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?

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 50
1 <= N <= 26

Time Limit: 1 sec
Sample Input 1:
2
5
4
Sample Output 1:
E
DE
CDE
BCDE
ABCDE

D
CD
BCD
ABCD
Explanation for Sample Input 1:
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’].
Sample Input 2:
2
3
2
Sample Output 2:
C
BC
ABC

B
AB
Hint

Check if you can manipulate the nested loops to print the result?

Approaches (1)
Brute Force

The idea is to use nested loops in such a way that yields the answer.

 

The steps are as follows:

  • Since we need to start from the Nth character until the 1st character, we will try thinking backward.
  • We will iterate from i = N to i = 1. For each row in the given matrix, we will perform the following operations:
    • We will iterate through from j=i to j=N since we need to print the characters in lexicographical order in each row.
      • So we will add the characters: ‘A’+j-1 into our temporary vector.
      • Also, after each iteration of the inner loop completes, add the temporary vector into our final answer 2D matrix.
  • We will return the matrix as the final answer.
Time Complexity

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).

Space Complexity

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).

Code Solution
(100% EXP penalty)
Interesting Alphabets
Full screen
Console