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 ':'.This approach involves parsing the input string to extract the start and end columns and rows. We then use nested loops; the outer loop traverses through the column range, and the inner loop processes the row range. For each combination of column and row, we construct the cell name and add it to the result list.
This solution extracts the starting and ending columns and rows from the input string. We calculate the total number of cells and allocate memory accordingly. Using two nested loops, we generate the cell names and store them in the result array.
C++
Java
Python
C#
JavaScript
Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) where c1 and c2 are the start and end columns, and r1 and r2 are the start and end rows.
Space Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) as we store all cell names in a list.
This approach involves extracting the individual ranges for columns and rows and using a Cartesian product to generate all possible cell combinations within the range.
This code utilizes a basic Cartesian product logic by iterating over start and end ranges for both columns and rows, thus combining them into all possible combinations.
C++
Java
Python
C#
JavaScript
Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1))
Space Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1))
| Approach | Complexity |
|---|---|
| Iterate through Columns and Rows | Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) where c1 and c2 are the start and end columns, and r1 and r2 are the start and end rows. |
| Using Cartesian Product | Time Complexity: O((c2 - c1 + 1) * (r2 - r1 + 1)) |
How to automatically write date in Excel tips and tricks 💯💫 #exceltips #tutorial #shortvideo • The SanBytes • 2,098,896 views views
Watch 9 more video solutions →Practice Cells in a Range on an Excel Sheet with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor