Matrix Letter Challenge
Given a matrix of characters where each cell contains a lowercase letter, determine the longest contiguous line (either horizontal, vertical, or diagonal) of repeated characters and return that letter along with the length as a formatted string. Example output: a-4 means the letter 'a' is the longest with a repeated line of length 4 in any direction.
[ [ "a", "b", "c" ], [ "d", "c", "b" ], [ "g", "b", "b" ] ]
Explanation. The letter 'b' forms the longest contiguous line of 3 vertically on the right side of the matrix.
[ [ "z", "z", "z" ], [ "b", "z", "a" ], [ "c", "d", "e" ] ]
Explanation. The letter 'z' forms the longest contiguous horizontal line at the top of the matrix.
[ [ "x", "y", "z" ], [ "a", "x", "c" ], [ "b", "w", "x" ] ]
Explanation. The letter 'x' forms the longest diagonal from the top left to the bottom right.
Follow-up: Can you optimize your solution to minimize the number of passes you need to do through the matrix?
The input matrix will be a non-empty 2D array of lowercase letters. Minimum matrix size will be 1x1.
- Accepted
- 0/1
- Acceptance Rate
- 0.0%
- Views
- 2