1. Map row → end. Row 1 ends at chr(ord('A') + 0), row 2 at chr(ord('A') + 2), row 3 at chr(ord('A') + 4), and so on.
2. Print the prefix. Inner loop still walks A..$end on every row.
3. Stay in A–Y. Clamp $rows to 1–13 so $end never walks past Y.
Edge Cases & Pitfalls
Check these before calling the solution done.
$i++
Even lengths
If the outer loop uses $i++ instead of stepping by 2, you get A, AB, ABC, … — not the odd-length pattern.
$i += 2
Coerced to int
$i += 2 on a letter string coerces it to an integer and breaks the outer loop. Use $i = chr(ord($i) + 2).
Inner starts at $i
Missing prefix
Always start the inner loop at 'A'. Starting at $i prints a single letter per row.
PHP_EOL inside
Column of letters
If echo PHP_EOL sits inside the inner loop, each letter lands on its own line. Call it only after the inner loop.
Even end letter
Messy last row
Prefer odd-step endings (A, C, E, …, Y). An even letter like D still runs but breaks the clean odd-width story.
Past Y
Clamp to 13 rows
Row 13 ends at Y. Larger values walk past Z — clamp or stop early.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char step / row-count forms
O($rows²)
O(1)
Total letters printed = 1 + 3 + … + (2r - 1) = r², which is quadratic in the number of rows.
Remember
Key Takeaways
Odd ends: outer letter steps A, C, E, … with chr(ord($i) + 2).
Fresh prefix: every row prints A..end from scratch.
Break the row:echo for letters; echo PHP_EOL after the inner loop.
Complexity:O(r²) time because total letters equal r²; O(1) extra space.
One line: for each end letter stepping by two, echo A..end, then echo PHP_EOL.
Frequently Asked Questions
It makes the ending letter jump by two (A, C, E, ...) so each row length increases by two characters and stays odd. In PHP write $i = chr(ord($i) + 2).
The outer loop advances the ending letter by 2 (A, C, E, G, I). The inner loop prints every letter from A through that ending letter, so the count is always odd.
Because each row is a fresh prefix A..end. Starting at the end letter would skip earlier letters and change the pattern.
echo sends output without adding a line break. echo PHP_EOL ends the current line with a portable newline. Letters use echo; the row break uses echo PHP_EOL after the inner loop.
1+3+…+(2r-1)=r². For 5 rows that is 25 letters.
O(r²) for r rows, because total echo calls equal r².
The loop still runs, but you no longer get a clean set of odd-length rows aligned to A, C, E, …. Prefer an odd-step ending letter (A, C, E, …, Y) for this pattern.
Prefer reading a row count with trim(fgets(STDIN)) + is_numeric(), or validate a single letter from the input string, and keep the ending letter within A–Z.
🤔
Did you know?
Odd numbers add up to perfect squares: 1+3+5+…+(2r-1)=r². That is why this pattern prints exactly r² letters for r rows — the same count that makes the complexity O(r²).