Leetcode #2194: Cells in a Range on an Excel Sheet
In this guide, we solve Leetcode #2194 Cells in a Range on an Excel Sheet in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
A cell (r, c) of an excel sheet is represented as a string "
" where: denotes the column number c of the cell. It is represented by alphabetical letters.
Quick Facts
- Difficulty: Easy
- Premium: No
- Tags: String
Intuition
We need to scan characters while tracking positions or counts.
A simple state machine keeps the logic precise.
Approach
Iterate through the string once and update the state for each character.
Use a map or array if you need fast lookups.
Steps:
- Iterate through characters.
- Maintain necessary state.
- Build or validate the output.
Example
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.
Python Solution
class Solution:
def cellsInRange(self, s: str) -> List[str]:
return [
chr(i) + str(j)
for i in range(ord(s[0]), ord(s[-2]) + 1)
for j in range(int(s[1]), int(s[-1]) + 1)
]
Complexity
The time complexity is , and the space complexity is , where and are the range of rows and columns, respectively. The space complexity is , where and are the range of rows and columns, respectively.
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.