Solving Leetcode Interviews in Seconds with AI: Cells in a Range on an Excel Sheet
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2194" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
A cell (r, c) of an excel sheet is represented as a string "
" where: 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. 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 ':'.
Explanation
Here's a breakdown of the solution:
- Parse Input: Extract the column and row ranges from the input string. Convert the column letters into numerical representation.
- Iterate and Generate: Iterate through all rows and columns within the specified ranges, creating the cell string representation for each cell.
Store and Return: Store each generated cell string in a list and return the list.
Runtime Complexity: O(R * C), where R is the number of rows and C is the number of columns in the range.
- Storage Complexity: O(R * C), to store the generated cell strings.
Code
def cellsInRange(s: str) -> list[str]:
"""
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_col = s[0]
start_row = int(s[1])
end_col = s[3]
end_row = int(s[4])
result = []
for col in range(ord(start_col), ord(end_col) + 1):
for row in range(start_row, end_row + 1):
result.append(chr(col) + str(row))
return result