2194. Cells in a Range on an Excel Sheet LeetCode Solution
In this guide, you will get 2194. Cells in a Range on an Excel Sheet LeetCode Solution with the best time and space complexity. The solution to Cells in a Range on an Excel Sheet problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Cells in a Range on an Excel Sheet solution in C++
- Cells in a Range on an Excel Sheet solution in Java
- Cells in a Range on an Excel Sheet solution in Python
- Additional Resources

Problem Statement of Cells in a Range on an Excel Sheet
A cell (r, c) of an excel sheet is represented as a string “
For example, the 1st column is denoted by ‘A’, the 2nd by ‘B’, the 3rd by ‘C’, and so on.
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 “:”, where represents the column c1, represents the row r1, represents the column c2, and 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 ':'.
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
2194. Cells in a Range on an Excel Sheet LeetCode Solution in C++
class Solution {
public:
vector<string> cellsInRange(string s) {
vector<string> ans;
const char startCol = s[0];
const char endCol = s[3];
const char startRow = s[1];
const char endRow = s[4];
for (char col = startCol; col <= endCol; ++col)
for (char row = startRow; row <= endRow; ++row)
ans.push_back(string(1, col) + row);
return ans;
}
};
/* code provided by PROGIEZ */
2194. Cells in a Range on an Excel Sheet LeetCode Solution in Java
class Solution {
public List<String> cellsInRange(String s) {
List<String> ans = new ArrayList<>();
final char startCol = s.charAt(0);
final char endCol = s.charAt(3);
final char startRow = s.charAt(1);
final char endRow = s.charAt(4);
for (char col = startCol; col <= endCol; ++col)
for (char row = startRow; row <= endRow; ++row)
ans.add("" + col + row);
return ans;
}
}
// code provided by PROGIEZ
2194. Cells in a Range on an Excel Sheet LeetCode Solution in Python
class Solution:
def cellsInRange(self, s: str) -> list[str]:
ans = []
startCol, startRow, _, endCol, endRow = s
for j in range(ord(startCol), ord(endCol) + 1):
for i in range(int(startRow), int(endRow) + 1):
ans.append(chr(j) + str(i))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.