Problem overview
The method printStars receives a positive integer n and prints a triangle made of stars. It prints n lines, and line number i (counting from 1) holds exactly i stars. It returns nothing, which is why its return type is void: the output is what the method prints, not a value it hands back.
For n = 5, the output is:
*
**
***
****
*****
Approach: grow one row into the next
You could build every row from scratch, but there is a simpler observation: each row is the previous row plus one more star. Row 3 is row 2 with a star added at the end.
So keep one string, str, that starts empty. Repeat n times: add a star to str, then print it. The first pass prints one star, the second prints two, and so on. No counting of stars is needed, because the string itself carries the count.
Step by step
- Declare the row. Create
strwithlet, because it changes on every pass. Give it the typestringand start it as"". - Loop once per row. Write
for (let i = 0; i < n; i++). It runs exactlyntimes, foriequal to 0, 1, ...,n - 1. The counterionly counts the rows. Its value is never used inside the loop. - Add a star. Inside the loop,
str += "*"appends one star to the end of the current row. - Print the row. Still inside the loop, after the star was added, call
console.log(str). Each call ends the line, so every row gets its own line. Printing before adding the star would make the first row empty, and printing after the loop would show only the last row.
The solution in TypeScript
class Solution {
printStars(n: number): void {
let str: string = "";
for (let i = 0; i < n; i++) {
str += "*";
console.log(str);
}
}
}
The TypeScript details worth a closer look:
voidmeans no result. The method has noreturn. The printing happens as a side effect ofconsole.log. Callingnew Solution().printStars(3)prints three lines.numberfor every number. The parameternis anumber, the one numeric type used for integers and decimals alike. The loop works as the statement intends for whole numbers.+=on a string. The right side is joined to the end of the old string, and the new string is stored instr. Strings are immutable, so a new string is created each time. For rows this short, that is no concern.letandconst.stris reassigned, so it needslet. The loop counter is also declared withletbecausei++reassigns it.console.logadds the line break. You don't write"\n"yourself. The string you pass has no line break in it, and each call prints its argument followed by a new line.
Complexity
Time: O(n²). The loop runs n times, and printing the row on pass k writes k characters. The total number of characters printed is 1 + 2 + ... + n, which is n(n + 1) / 2. Any solution has to print that many stars, so the quadratic time comes from the size of the output.
Space: O(n). The only thing stored is str, which grows to n characters.
Edge cases and common mistakes
n = 1. The loop runs once and prints a single*.- Printing after the loop. Only the last row,
*****forn = 5, is shown. - Printing before adding the star. The first line is empty, and the last row is missing.
- Resetting
strinside the loop. If the declaration sits inside the loop, every row has just one star. - Off-by-one bounds.
i <= nwithistarting at 0 printsn + 1rows. - Returning a string instead of printing. The method's return type is
void, and the lesson's tests look at what is printed.
Testing
n = 1prints one line:*.n = 3prints*,**,***, each on its own line.n = 5prints the triangle from the example.