AlgoViz

Pattern 16

Easy

Row i repeats the i-th letter

Problem

Print a triangle where row i repeats the i-th letter for a given size n.

In simple words

Use one loop for the rows and another for what goes in each row to draw the shape.

The idea

The row index chooses the letter and the row index also sets the count, so both loops read from the outer counter. It is the alphabet version of pattern 4.

The trick

  • Print `'A' + i - 1`, repeated i times.
rows5

Step 1 of 7. Row i repeats the i-th letter, i times. rows 5.

1/7
Optimal
timeO(n^2)spaceO(1)
1// n = 5 produces:2// A3// B B4// C C C5// D D D D6// E E E E E7 8for i in 1..n:9  for j in 1..i: print(char('A'+i-1))10  newline

Input

grid
5 × 5

Memory

rows
5

Output

printed rows
rows
5

Check yourself

3 quick questions about this walkthrough. A wrong answer costs nothing.

Example

Input:
n = 5
Output:
A / B B / C C C / D D D D / E E E E E

Finished the walkthrough? Add it to your streak.