Matrix Traversal Strategies
Given an integer matrix and a strategy code, return all matrix elements in the order defined by the selected traversal strategy.
Represent each matrix row as a comma-separated string. The supported strategy codes are "SPIRAL", "ROW", and "COLUMN".
Implement the solution using the Strategy Design Pattern. Each traversal strategy should contain its own traversal logic, and the requested strategy should be selected using the supplied strategy code.
Method Signature
List<Integer> traverseMatrix(List<String> matrix, String strategyCode)
Parameters
matrix contains the matrix rows. Each row is represented as a comma-separated string of integers.
strategyCode identifies the traversal strategy to use. It will always be one of "SPIRAL", "ROW", or "COLUMN".
Return Value
Return a one-dimensional list containing every matrix element exactly once in the order defined by strategyCode.
Traversal Strategies
SPIRAL
Traverse the matrix in clockwise spiral order, beginning with the top-left element and moving inward one boundary at a time.
- Traverse the top boundary from left to right.
- Traverse the right boundary from top to bottom.
- Traverse the remaining bottom boundary from right to left.
- Traverse the remaining left boundary from bottom to top.
- Repeat these steps for the remaining inner matrix.
ROW
Traverse the matrix row by row. Process rows from top to bottom and elements within each row from left to right.
COLUMN
Traverse the matrix column by column. Process columns from left to right and elements within each column from top to bottom.
Design Requirements
- Define a common traversal strategy interface.
- Implement separate spiral, row-by-row, and column-by-column strategy classes.
- Select the appropriate strategy using
strategyCode.
- The main traversal method must use the common strategy interface instead of directly implementing each traversal pattern.
- Adding another traversal strategy should not require changing the existing concrete traversal strategies.
- The traversal must not modify the supplied matrix.
Constraints
1 <= matrix.size() <= 1,000
- Every string in
matrix contains the same number of comma-separated integers.
1 <= columns <= 1,000
1 <= matrix.size() * columns <= 100,000
-1,000,000,000 <= matrix value <= 1,000,000,000
- Each matrix row contains comma-separated integers without spaces.
strategyCode is case-sensitive.
strategyCode is "SPIRAL", "ROW", or "COLUMN".
Examples
Example 1: Spiral Traversal
traverseMatrix(matrix = List.of("1,2,3,4", "5,6,7,8", "9,10,11,12"), strategyCode = "SPIRAL")
Output: List.of(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7)
The outer boundary is visited first, followed by the remaining inner elements.
Example 2: Row-by-Row Traversal
traverseMatrix(matrix = List.of("4,8,12", "16,20,24", "28,32,36"), strategyCode = "ROW")
Output: List.of(4, 8, 12, 16, 20, 24, 28, 32, 36)
Each row is processed from left to right before moving to the next row.
Example 3: Column-by-Column Traversal
traverseMatrix(matrix = List.of("4,8,12", "16,20,24", "28,32,36"), strategyCode = "COLUMN")
Output: List.of(4, 16, 28, 8, 20, 32, 12, 24, 36)
Each column is processed from top to bottom before moving to the next column.