Skip to content

Latest commit

 

History

History
 
 

2194

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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.
    <ul>
    	<li>For example, the <code>1<sup>st</sup></code> column is denoted by <code>'A'</code>, the <code>2<sup>nd</sup></code> by <code>'B'</code>, the <code>3<sup>rd</sup></code> by <code>'C'</code>, and so on.</li>
    </ul>
    </li>
    <li><code>&lt;row&gt;</code> is the row number <code>r</code> of the cell. The <code>r<sup>th</sup></code> row is represented by the <strong>integer</strong> <code>r</code>.</li>
    

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 ':'.

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/
// Author: github.com/lzl124631x
// Time: O(1) since there are at most 26 * 9 cells.
// Space: O(1) extra space
class Solution {
public:
    vector<string> cellsInRange(string s) {
        vector<string> ans;
        char a = s[0], b = s[3], x = s[1], y = s[4];
        for (; a <= b; ++a) {
            for (char i = x; i <= y; ++i) {
                ans.push_back(string(1, a) + string(1, i));
            }
        }
        return ans;
    }
};

Discuss

https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/discuss/1823631