Skip to main content

Cells in a Range on an Excel Sheet - Solution & Explanation

EasyString12 min read
Practice this problem

Problem Statement

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.

 

Example 1:

Input: s = "K1:L2"
Output: ["K1","K2","L1","L2"]
Explanation:
The above diagram shows the cells which should be present in the list.
The red arrows denote the order in which the cells should be presented.

Example 2:

Input: s = "A1:F1"
Output: ["A1","B1","C1","D1","E1","F1"]
Explanation:
The above diagram shows the cells which should be present in the list.
The red arrow denotes the order in which the cells should be presented.

 

Constraints:

  • s.length == 5
  • 'A' <= s[0] <= s[3] <= 'Z'
  • '1' <= s[1] <= s[4] <= '9'
  • s consists of uppercase English letters, digits and ':'.

Approach Overview

Problem Overview: The input is a string like "A1:C3" representing a rectangular range on an Excel sheet. You need to return every cell coordinate inside that range in lexicographic order (column first, then row). Each coordinate is formatted like A1, B2, etc.

Approach 1: Iterate through Columns and Rows (Time: O(k), Space: O(1) excluding output)

The range always contains a starting column, starting row, ending column, and ending row. Extract these four values directly from the string. Then iterate column-by-column from the start column to the end column, and inside that loop iterate row-by-row from the start row to the end row. For every pair, construct the coordinate string using the column character and row digit. This works because Excel ranges expand in a simple grid pattern, so a nested loop naturally generates the correct order.

This approach is essentially a small grid traversal. The total work depends on the number of cells in the range (k). Since the problem guarantees single-letter columns and single-digit rows, parsing is constant time and the nested iteration is straightforward. This is the most direct and commonly used solution for problems involving string parsing and simple simulation.

Approach 2: Using Cartesian Product (Time: O(k), Space: O(k))

Another way to view the problem is as a Cartesian product between the column range and the row range. First generate the list of columns (for example A, B, C) and the list of rows (1, 2, 3). Then combine every column with every row to produce the final coordinates. Many languages provide helpers for Cartesian products (such as itertools.product in Python), which makes this approach concise.

The key idea is separating the two dimensions of the grid before combining them. While the time complexity is still O(k), this version often creates intermediate lists for rows and columns, which slightly increases auxiliary memory usage. It is conceptually clean and useful when solving broader combinatorial or grid enumeration problems.

Recommended for interviews: The nested column-row iteration is what interviewers typically expect. It demonstrates clear string parsing and controlled iteration without unnecessary abstractions. The Cartesian product approach is equally correct but may feel like overengineering unless the language provides a very concise helper.

Approach 1: Iterate through Columns and Rows

This approach involves parsing the input string to extract the start and end columns and rows. We then use nested loops; the outer loop traverses through the column range, and the inner loop processes the row range. For each combination of column and row, we construct the cell name and add it to the result list.

This solution extracts the starting and ending columns and rows from the input string. We calculate the total number of cells and allocate memory accordingly. Using two nested loops, we generate the cell names and store them in the result array.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) where c1 and c2 are the start and end columns, and r1 and r2 are the start and end rows.
Space Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) as we store all cell names in a list.

Try this approach in the editor →

Approach 2: Using Cartesian Product

This approach involves extracting the individual ranges for columns and rows and using a Cartesian product to generate all possible cell combinations within the range.

This code utilizes a basic Cartesian product logic by iterating over start and end ranges for both columns and rows, thus combining them into all possible combinations.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1))
Space Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1))

Try this approach in the editor →

Approach 3: Simulation

We directly traverse all the cells within the range and add them to the answer array.

The time complexity is O(m times n), and the space complexity is O(m times n), where m and n are the range of rows and columns, respectively.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterate through Columns and Rows

Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) where c1 and c2 are the start and end columns, and r1 and r2 are the start and end rows.
Space Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) as we store all cell names in a list.

Using Cartesian Product

Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1))
Space Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1))

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterate through Columns and RowsO(k)O(1)Best general solution. Simple nested loops generate cells directly in lexicographic order.
Cartesian Product of Rows and ColumnsO(k)O(k)Useful when a language provides product utilities or when modeling the grid as two independent sets.

Video Solution

2194. Cells in a Range on an Excel Sheet || Leetcode Weekly Contest 283 || Leetcode 2194 • Bro Coders • 2,483 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Cells in a Range on an Excel Sheet easy or hard?
Cells in a Range on an Excel Sheet is classified as an Easy problem on LeetCode with an acceptance rate above 80%. The task focuses on basic string parsing and simple nested iteration, making it a common warm-up problem for beginners practicing array or string manipulation.
Cells in a Range on an Excel Sheet Python/Java solution
Both Python and Java implementations follow the same idea: parse the four boundary characters from the range string and run nested loops over columns and rows. Python often uses character iteration with chr/ord or itertools.product, while Java typically uses char loops and string concatenation. Both achieve O(k) time complexity.
How to solve Cells in a Range on an Excel Sheet in O(n)?
Treat the Excel range as a small grid. Extract the start column, end column, start row, and end row from the string, then use two loops: the outer loop iterates columns and the inner loop iterates rows. For each pair, concatenate the column character and row digit to form the cell name. This generates all cells in O(k) time where k is the number of coordinates produced.
What is the best approach for Cells in a Range on an Excel Sheet?
The most practical solution iterates through columns and rows using nested loops. Parse the starting and ending column and row from the input string, then generate each coordinate by combining the current column and row. This runs in O(k) time where k is the number of cells in the range and uses O(1) extra space aside from the output list.
Is Cells in a Range on an Excel Sheet asked at Google/Amazon/Meta?
This problem is categorized as an easy string parsing and simulation question. Variations of grid enumeration and coordinate generation appear in interviews at companies like Amazon and Google, especially in early rounds where candidates demonstrate basic iteration and string handling skills.
What data structure is used in Cells in a Range on an Excel Sheet?
The solution primarily uses strings and a dynamic list or array to store the generated coordinates. The algorithm relies on simple iteration rather than advanced data structures, making it a straightforward string manipulation and simulation problem.
What is the time complexity of Cells in a Range on an Excel Sheet?
The time complexity is O(k), where k is the number of cells inside the specified Excel range. Each cell coordinate is generated exactly once by iterating over the column range and row range. Space complexity is O(1) auxiliary space, not counting the output list.

Ready to solve this problem?

Practice Cells in a Range on an Excel Sheet with our built-in code editor and test cases.

Practice on FleetCode