Last Updated: 25 Nov, 2020

Ninja and characters

Easy
Asked in companies
Nagarro SoftwareInnostax Software LabsWatchGuard Technologies

Problem statement

Ninja loves playing with characters. So one day, he wants to arrange a few characters in the ‘N’ number of rows. The first row contains 1 character, the second row has two characters, the third row has 3 characters, and so on.

Ninja starts placing characters in increasing order, with an absolute difference of 1, starting from A and can go up to Z only.

You are given an integer ‘N’ denoting the given number of rows. Can you print the pattern Ninja wants to create?

Pattern for N = 3

A
BB
CCC
Input Format:
The first line contains ‘T’, denoting the number of test cases.

Each test case contains a single integer ‘N’, denoting the number of rows.
Output Format:
For each test case, print 'N' strings denoting the required pattern in the following ‘N’ lines.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 26

Time Limit: 1 sec

Approaches

01 Approach

The key here is to traverse all the lines sequentially, and for each line, we print the required character at the given index.


 

The steps are as follows:

  • We define an ‘ans’ matrix to store the final pattern.
  • We initialize a variable ‘k’ to 0, which will be the starting value for the pattern.
  • We will iterate over all the rows, i.e., i = 0 to i = N - 1:
    • We will run a for loop starting from j = 0 with the condition j is less than or equals to i.
      • We will insert ‘k’ in the matrix ‘ans’.
      • We will increment the value of k.
  • We will return the matrix ‘ans’ as the final answer.