A symmetric decreasing alphabet square prints fixed-width mirrored rows whose interior floor drops from the top letter down to a single center A.
Remember
Rule: for floor i from k down to 0,
left j = k..0 and right j = 1..k;
print j if j > i, else print i
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E ← top = 'E' (k = 4, width 9)
The right half starts at j = 1 (B) so the center A is not duplicated. Program 29 reuses this row rule in two phases to form a full diamond.
Approach
How to Solve It
Two ways to emit the same square — inline floor checks, then optionally a shared helper for both halves.
Method
Idea
Best for
Inline $j > $i
Left $k..0 + right 1..$k with the same ternary
Learning, interviews, exams
Helper function
One print_cell owns the floor rule
Cleaner demos once the rule clicks
Pseudocode
Pseudocode
$k = ord($top) - ord('A')
for $i from $k down to 0:
for $j from $k down to 0:
echo ($j > $i ? $alpha[$j] : $alpha[$i])
for $j from 1 to $k:
echo ($j > $i ? $alpha[$j] : $alpha[$i])
echo PHP_EOL
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
How It Works
1. Own the rule once.print_cell applies $j > $i in a single place.
2. Thin loops. Left and right loops only decide which columns to visit.
3. Same shape. Output matches Examples 1–2 — useful when you want to explain the floor rule separately.
Edge Cases & Pitfalls
Check these before calling the solution done.
j = 0
Duplicate center A
If the right half starts at 0, the last row prints A twice. Keep for ($j = 1; $j <= $k; $j++).
j >= i
Wrong borders
Using $j >= $i changes which cells belong to the floor vs the border. Stick to strict $j > $i.
PHP_EOL inside
Column of letters
If echo PHP_EOL is inside either half-loop, each cell lands on its own line. Use echo for cells; end the row only after both halves.
top = A
Single A
When $k = 0, output is just A — right half never runs. A good sanity check.
top < A
Empty / invalid
Validate A–Z before computing $k.
Bad input
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline loops (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Helper function (Example 3)
O(n²)
O(1)
For $n = $k + 1 rows of width 2 * $k + 1, total cells = $n × (2n - 1) — still quadratic in n.
Remember
Key Takeaways
Rule: for floor $i, print $j > $i ? $alpha[$j] : $alpha[$i] on both halves.
Mirror cleanly: left $k..0, right 1..$k — skip duplicating center A.
Break the row: call echo PHP_EOL only after both half-loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each floor $i from top down to A, scan left $k..0 and right 1..$k, printing $j > $i ? $alpha[$j] : $alpha[$i], then echo PHP_EOL.
Frequently Asked Questions
It prints the border letters when the column letter j is above the current row floor i; otherwise it prints i. This builds higher-letter borders with a flat interior.
The first loop scans E down to A. The second scans B up to E so the middle A is printed once and the row is mirrored.
For letters A..k, width is (k-A+1) + (k-A) = 2*(k-A)+1. For A..E, width is 9.
Pick a larger top letter (like H), set $k = ord($top) - ord('A'), and keep the same loop structure.
O(n²) for n letters because there are n rows and each row prints O(n) cells.
Starting at 0 would print $alpha[0] (A) again and duplicate the center. Starting at 1 (B) mirrors the left half cleanly.
echo stays on the same line. echo PHP_EOL ends the current line. Letters use echo; the row break uses echo PHP_EOL after both half-loops.
Use trim(fgets(STDIN)), take the first character after strtoupper, require A–Z, and reject empty or multi-character tokens.
🤔
Did you know?
Fix k at the top letter (E). Outer loop i goes from k down to A. Left half scans j = k..A; right half scans j = B..k so A appears once in the middle. Each position prints j when j > i, otherwise prints i.