Skip to main content

Pyramid of Stars

EasyProblem

Description

Given n, return a pyramid of stars, n rows high, as a list of rows. Row i, counting from 1, has 2i - 1 stars, centred: the last row has 2n - 1 stars and starts at the edge, and each row above it is indented one more space. No row has spaces after its stars.

Examples

Example 1

Input: n = 3

Output: [" *", " ", "**"]

Explanation: Row 1 is 2 spaces and 1 star, row 2 is 1 space and 3 stars, row 3 is 5 stars.

Example 2

Input: n = 1

Output: ["*"]

Explanation: One row with one star.

Example 3

Input: n = 4

Output: [" *", " *", " *****", "*****"]

Explanation: The widest row, the last, has 2 × 4 - 1 = 7 stars.

Constraints

  • 1 ≤ n ≤ 100

Editorial

Approach - Build Each Row from Its Counts

Intuition

Each row is two counts: spaces, then stars. The stars grow by 2 a row, one on each side, so row i has 2i - 1. The last row starts at the edge, and each row above it sits one space further in, so row i starts with n - i spaces. Nothing goes after the stars.

Step-by-Step Explanation

Let's trace Example 1, n = 3:

  1. Row 1: 2 spaces, then 1 star.
  2. Row 2: 1 space, then 3 stars.
  3. Row 3: 0 spaces, then 5 stars.
  4. Return the 3 rows.

Pyramid of Stars — The rows, built one per step from the top; the row just built is lit.

Algorithm

  1. For i from 1 to n, add a row of n - i spaces followed by 2i - 1 stars.
  2. Return the rows.

Code

class Solution {
public:
    vector<string> pyramid(int n) {
        vector<string> rows;
        for (int i = 1; i <= n; i++) rows.push_back(string(n - i, ' ') + string(2 * i - 1, '*'));
        return rows;
    }
};
from typing import List

class Solution:
    def pyramid(self, n: int) -> List[str]:
        return [" " * (n - i) + "*" * (2 * i - 1) for i in range(1, n + 1)]
import java.util.*;

class Solution {
    public List<String> pyramid(int n) {
        List<String> rows = new ArrayList<>();
        for (int i = 1; i <= n; i++) rows.add(" ".repeat(n - i) + "*".repeat(2 * i - 1));
        return rows;
    }
}
var pyramid = function(n) {
    const rows = [];
    for (let i = 1; i <= n; i++) rows.push(" ".repeat(n - i) + "*".repeat(2 * i - 1));
    return rows;
};

Complexity Analysis

Time Complexity: O(n²)

There are O(n) rows of O(n) characters, and each character is written once.

Space Complexity: O(n²)

The rows of the answer.

Common Mistakes

Each of these looks right on a quick read, and the tests still reject it. The outputs below come from running the code.

Giving row i only i stars. "*" * i

On n = 3 it returns [" *", " **", "***"]; the answer is [" *", " ***", "*****"]. Row i of a pyramid has 2i - 1 stars, so each row grows by 2, one on each side. With i stars the rows are only half as wide and the pyramid leans.

Padding the right side too. + " " * (n - i)

On n = 3 it returns [" * ", " *** ", "*****"]; the answer is [" *", " ***", "*****"]. Spaces after the stars are invisible, but they're part of the string, and the answer has none. Only the spaces before the stars centre the row.

Indenting one space too many. " " * (n - i + 1)

On n = 3 it returns [" *", " ***", " *****"]; the answer is [" *", " ***", "*****"]. The last row, the widest, starts at the edge with no space before it, so row i needs n - i spaces, not n - i + 1.

Where This Pattern Is Taught

This problem practises nested counting: for each row, how many of each character, and building the row as a string. It's taught in Python for DSA, Java for DSA, C++ for DSA and JavaScript for DSA.