Foundations Loops through pattern printing

21. Letter Pyramid

Easy

Given an integer n, return the pattern below as a list of strings, one string per row, top row first.

   A
  ABA
 ABCBA
ABCDCBA

The picture shows n = 4. The pyramid has n rows. Row i, counting from 1 at the top, climbs from A up to the i-th capital letter and comes back down to A, so the top row is a single A and the bottom row climbs to the n-th letter. The highest letter of a row appears once, in the middle of the row.

Every row is centred over the bottom row, which starts at the left edge. The letters sit side by side, and nothing comes after the last A.

Example 1

Input: n = 3

Output:
  A
 ABA
ABCBA

Explanation: The rows climb to A, B and C, after 2, 1 and 0 spaces. Each row ends at its last A.

Example 2

Input: n = 1

Output:
A

Example 3

Input: n = 5

Output:
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Check your understanding

Work out the answer for this input before you write any code.

n = 3

Constraints

  • 1 ≤ n ≤ 26

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.