AlgoMaster Logo

Alphabet Pyramid

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

This pattern climbs through the alphabet instead of digits. Row 1 shows A, row 2 shows AB, row 3 shows ABC, and each new row is one letter longer than the one above it. By row n the line reads from A to the n-th letter.

What makes this an alphabet problem is how letters are stored. A character is a small integer code, and the uppercase letters sit next to each other in that scheme, so A is followed by B, then C. That ordering lets us walk the alphabet with arithmetic: take the code for A, add 1 to reach B, add 2 to reach C, and add j to reach the (j + 1)-th letter.

The rest is the same counting logic as any triangular pattern. Row i needs the first i letters, produced at positions 0 through i - 1 and joined into a single string.

Key Constraints:

  • 1 <= n <= 26n is at least 1, so there is always at least the row A. The upper bound of 26 matters because the alphabet has exactly 26 uppercase letters, and the constraint guarantees we never run past Z.
  • Row i has i letters → The first row has one letter and each row below adds exactly one more, so the rows grow by one character at a time.
  • Letters start at A → Every row begins at A and counts forward, so the left edge of the pyramid is always the same letter.

Approach 1: Build Each Row With Character Arithmetic

Intuition

Build each row on its own. For row i, start a fresh string, walk j from 0 up to i - 1, compute the letter A + j, and append it.

This uses two loops. The outer loop picks the row, the inner loop produces its letters one at a time. Nothing carries over from one row to the next.

Algorithm

  1. Create an empty list to hold the rows.
  2. For i from 1 up to n, start an empty string for the current row.
  3. For j from 0 up to i - 1, compute the letter whose code is the code of A plus j, and append it to the row.
  4. Add the finished row string to the list.
  5. Return the list of rows.

Example Walkthrough

Input:

3
n

The outer loop runs for i = 1, 2, 3. At i = 1, the inner loop runs only for j = 0, giving the letter A + 0 = A, so the row is "A". At i = 2, the inner loop runs for j = 0, 1, giving A and A + 1 = B, so the row is "AB". At i = 3, the inner loop runs for j = 0, 1, 2, giving A, B, and A + 2 = C, so the row is "ABC". The three rows collect into the full pyramid:

0
A
1
AB
2
ABC
result

Code

Rebuilding every row from scratch repeats work, since each row already contains all the letters of the row above it. We can use that overlap and grow the pyramid one letter at a time.

Approach 2: Grow Each Row From the Previous One

Intuition

Row 3 is "ABC" and row 4 is "ABCD", the previous row with one more letter on the end. That holds all the way down: every row is the previous row plus the next letter.

So keep a running string. Begin it empty, and on each step append the next letter and record the string as the new row. The letter on step i is A + (i - 1).

Algorithm

  1. Create an empty list to hold the rows.
  2. Start with an empty running string.
  3. For i from 1 up to n, append the letter whose code is the code of A plus i - 1 to the running string.
  4. Add the current running string to the list as the next row.
  5. Return the list of rows.

Example Walkthrough

Input:

3
n

The running string starts empty. At i = 1, we append A + 0 = A, so the string becomes "A" and that is row 1. At i = 2, we append A + 1 = B, so the string becomes "AB" and that is row 2. At i = 3, we append A + 2 = C, so the string becomes "ABC" and that is row 3. Each step adds a single letter and saves the result:

0
A
1
AB
2
ABC
result

Code