Skip to content

2194. Cells in a Range on an Excel Sheet 👍

  • Time: $O(mn)$
  • Space: $O(mn)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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