Cells in a Range on an Excel Sheet - Problem

A cell (r, c) of an excel sheet is represented as a string <col><row> where:

  • <col> denotes the column number c of the cell. It is represented by alphabetical letters.
    • For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.
  • <row> is the row number r of the cell. The rth row is represented by the integer r.

You are given a string s in the format <col1><row1>:<col2><row2>, where <col1> represents the column c1, <row1> represents the row r1, <col2> represents the column c2, and <row2> represents the row r2, such that r1 ≤ r2 and c1 ≤ c2.

Return the list of cells (x, y) such that r1 ≤ x ≤ r2 and c1 ≤ y ≤ c2. The cells should be represented as strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows.

Input & Output

Example 1 — Basic Range
$ Input: s = "K1:L2"
Output: ["K1","K2","L1","L2"]
💡 Note: Start at K1, end at L2. First process column K (rows 1-2): K1, K2. Then column L (rows 1-2): L1, L2.
Example 2 — Single Cell
$ Input: s = "A1:F1"
Output: ["A1","B1","C1","D1","E1","F1"]
💡 Note: Single row across multiple columns A through F, all at row 1.
Example 3 — Single Column
$ Input: s = "A1:A3"
Output: ["A1","A2","A3"]
💡 Note: Single column A with multiple rows 1 through 3.

Constraints

  • s.length == 5
  • 'A' ≤ s[0] ≤ s[3] ≤ 'Z'
  • '1' ≤ s[1], s[2], s[4] ≤ '9'
  • s[2] == ':'
  • s[1] ≤ s[4]

Visualization

Tap to expand
Excel Cell Range: Input → Process → OutputInput: "A1:B3"Excel Grid VisualizationA1B1A2B2A3B3AB123Output Array:["A1", "A2", "A3", "B1", "B2", "B3"]Column A first:A1, A2, A3Then Column B:B1, B2, B3Iterate column by column, then row by row within each column
Understanding the Visualization
1
Input
Range string 'A1:B3' specifying start and end cells
2
Process
Parse coordinates and iterate through all cells column by column
3
Output
Ordered list of cell strings: ['A1','A2','A3','B1','B2','B3']
Key Takeaway
🎯 Key Insight: Parse the range boundaries, then systematically generate cells column by column for proper sorting
Asked in
Microsoft 25 Google 15
12.5K Views
Medium Frequency
~8 min Avg. Time
543 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen