Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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.
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 ':'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"K1:L2"
"A1:F1"
excel-sheet-column-title)excel-sheet-column-number)matrix-cells-in-distance-order)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2194: Cells in a Range on an Excel Sheet
class Solution {
public List<String> cellsInRange(String s) {
List<String> ans = new ArrayList<>();
for (char i = s.charAt(0); i <= s.charAt(3); ++i) {
for (char j = s.charAt(1); j <= s.charAt(4); ++j) {
ans.add(i + "" + j);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2194: Cells in a Range on an Excel Sheet
func cellsInRange(s string) (ans []string) {
for i := s[0]; i <= s[3]; i++ {
for j := s[1]; j <= s[4]; j++ {
ans = append(ans, string(i)+string(j))
}
}
return
}
# Accepted solution for LeetCode #2194: Cells in a Range on an Excel Sheet
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)
]
// Accepted solution for LeetCode #2194: Cells in a Range on an Excel Sheet
/**
* [2194] Cells in a Range on an Excel Sheet
*
* 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 1^st column is denoted by 'A', the 2^nd by 'B', the 3^rd by 'C', and so on.
*
*
* <row> is the row number r of the cell. The r^th 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png" style="width: 250px; height: 160px;" />
* 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png" style="width: 500px; height: 50px;" />
* 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 ':'.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/
// discuss: https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn cells_in_range(s: String) -> Vec<String> {
let s = s.as_bytes();
(s[0]..=s[3])
.flat_map(|col| (s[1]..=s[4]).map(move |row| format!("{}{}", col as char, row as char)))
.collect::<Vec<_>>()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2194_example_1() {
let s = "K1:L2".to_string();
let result = vec_string!["K1", "K2", "L1", "L2"];
assert_eq!(Solution::cells_in_range(s), result);
}
#[test]
fn test_2194_example_2() {
let s = "A1:F1".to_string();
let result = vec_string!["A1", "B1", "C1", "D1", "E1", "F1"];
assert_eq!(Solution::cells_in_range(s), result);
}
}
// Accepted solution for LeetCode #2194: Cells in a Range on an Excel Sheet
function cellsInRange(s: string): string[] {
const ans: string[] = [];
for (let i = s.charCodeAt(0); i <= s.charCodeAt(3); ++i) {
for (let j = s.charCodeAt(1); j <= s.charCodeAt(4); ++j) {
ans.push(String.fromCharCode(i) + String.fromCharCode(j));
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.