Last Updated: June 7, 2026
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.
1 <= n <= 26 → n 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.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.A → Every row begins at A and counts forward, so the left edge of the pyramid is always the same letter.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.
i from 1 up to n, start an empty string for the current row.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.Input:
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:
n times, and row i does i units of work to place its letters, so the total work is 1 + 2 + ... + n, which grows with the square of n.n rows whose lengths add up to 1 + 2 + ... + n, so the stored characters are on the order of n^2.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.
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).
i from 1 up to n, append the letter whose code is the code of A plus i - 1 to the running string.Input:
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:
n steps costs up to n characters, so the totals still add up to the order of n^2.n rows whose lengths grow from 1 to n, which sums to roughly n^2 characters regardless of how the rows are built.