A harbor authority arranges n signal buoys in a single line, numbered 0 through n-1 from the shore outward. Buoy i continuously repeats its own blink code -- a short string of lowercase English letters, one letter per blink slot. If you imagine writing buoy i's code across row i of a grid (one letter per column, starting at column 0), the buoys together fill in a grid of letters; a row may be shorter than another, so some grid cells are simply undefined for that row.
The harbourmaster calls the arrangement mirror-true if, for every buoy i and every column j at which row i has a defined letter, two things hold: buoy j must exist (0 <= j < n), and the letter buoy j shows in its own column i must be defined and identical to the letter buoy i shows in column j. Informally, reading down the buoys' codes column-by-column must reproduce the same letters as reading along the buoys' codes row-by-row, wherever a letter is defined on either side.
Given the n blink codes, determine whether the arrangement is mirror-true.
Line 1: an integer n -- the number of buoys. The next n lines: the blink code of buoy i (0-indexed), a non-empty string of lowercase English letters.
Print true if the arrangement is mirror-true, or false otherwise.
Example 1
Input
3 aba bab aba
Expected
true
Explanation
Reading the three blink codes as rows of a grid gives row0='aba', row1='bab', row2='aba'. Column 0 read top-to-bottom is 'a','b','a' -- identical to row0. Column 1 is 'b','a','b' -- identical to row1. Column 2 is 'a','b','a' -- identical to row2. Every row matches its corresponding column, so the arrangement is mirror-true and the answer is `true`.
Example 2
Input
2 ab cd
Expected
false
Explanation
Row0 is 'ab' and row1 is 'cd'. Row0's character at column 1 is 'b', but buoy 1's character at column 0 is 'c', not 'b' -- the mirror fails at that cell, so the answer is `false`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →