Diamond Cipher Readout
EasyMatrix · HRT
grid is an n x n grid of single-character strings, where n is odd. Let m = n // 2 be the index of the centre row and column.
- Keep only the cells inside the diamond: cell
(row, col)is kept whenabs(row - m) + abs(col - m) <= m. The diamond's four tips touch the middle of each edge of the grid. - Read the kept cells by anti-diagonal: in increasing order of
row + col, and within the same anti-diagonal from the top row to the bottom row.
Return the characters in that reading order joined into one string.
Example 1
- Input:
- grid = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
- Output:
- "bdefh"
- Explanation
- The kept cells are the centre and its four neighbours. Reading by increasing row plus column gives
banddfirst, since both sit on the same anti-diagonal and are read top to bottom, then the centre, thenfandhon the next shared anti-diagonal.
Example 2
- Input:
- grid = [["A", "B", "C", "D", "E"], ["F", "G", "H", "I", "J"], ["K", "L", "M", "N", "O"], ["P", "Q", "R", "S", "T"], ["U", "V", "W", "X", "Y"]]
- Output:
- "CGKHLIMQNROSW"
- Explanation
- The diamond keeps 13 cells. Anti-diagonal 2 holds
C,G,K(read top to bottom), diagonal 3 holdsH,L, diagonal 4 holdsI,M,Q, diagonal 5 holdsN,R, and diagonal 6 holdsO,S,W.
Example 3
- Input:
- grid = [["x"]]
- Output:
- "x"
- Explanation
- A 1 x 1 grid is its own diamond, so the only character is returned.
Constraints
1 <= n <= 999, andnis oddgridhasnrows, each withnsingle-character strings