3484. Design Spreadsheet LeetCode Solution
In this guide, you will get 3484. Design Spreadsheet LeetCode Solution with the best time and space complexity. The solution to Design Spreadsheet problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Design Spreadsheet solution in C++
- Design Spreadsheet solution in Java
- Design Spreadsheet solution in Python
- Additional Resources
Problem Statement of Design Spreadsheet
A spreadsheet is a grid with 26 columns (labeled from ‘A’ to ‘Z’) and a given number of rows. Each cell in the spreadsheet can hold an integer value between 0 and 105.
Implement the Spreadsheet class:
Spreadsheet(int rows) Initializes a spreadsheet with 26 columns (labeled ‘A’ to ‘Z’) and the specified number of rows. All cells are initially set to 0.
void setCell(String cell, int value) Sets the value of the specified cell. The cell reference is provided in the format “AX” (e.g., “A1”, “B10”), where the letter represents the column (from ‘A’ to ‘Z’) and the number represents a 1-indexed row.
void resetCell(String cell) Resets the specified cell to 0.
int getValue(String formula) Evaluates a formula of the form “=X+Y”, where X and Y are either cell references or non-negative integers, and returns the computed sum.
Note: If getValue references a cell that has not been explicitly set using setCell, its value is considered 0.
Example 1:
Input:
[“Spreadsheet”, “getValue”, “setCell”, “getValue”, “setCell”, “getValue”, “resetCell”, “getValue”]
[[3], [“=5+7”], [“A1”, 10], [“=A1+6”], [“B2”, 15], [“=A1+B2”], [“A1”], [“=A1+B2”]]
Output:
[null, 12, null, 16, null, 25, null, 15]
Explanation
Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns
spreadsheet.getValue(“=5+7”); // returns 12 (5+7)
spreadsheet.setCell(“A1”, 10); // sets A1 to 10
spreadsheet.getValue(“=A1+6”); // returns 16 (10+6)
spreadsheet.setCell(“B2”, 15); // sets B2 to 15
spreadsheet.getValue(“=A1+B2”); // returns 25 (10+15)
spreadsheet.resetCell(“A1”); // resets A1 to 0
spreadsheet.getValue(“=A1+B2”); // returns 15 (0+15)
Constraints:
1 <= rows <= 103
0 <= value <= 105
The formula is always in the format "=X+Y", where X and Y are either valid cell references or non-negative integers with values less than or equal to 105.
Each cell reference consists of a capital letter from 'A' to 'Z' followed by a row number between 1 and rows.
At most 104 calls will be made in total to setCell, resetCell, and getValue.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: int)}|)
3484. Design Spreadsheet LeetCode Solution in C++
class Spreadsheet {
public:
Spreadsheet(int rows) {}
void setCell(string cell, int value) {
spreadsheet[cell] = value;
}
void resetCell(string cell) {
spreadsheet[cell] = 0;
}
int getValue(string formula) {
const int i = formula.find('+');
return getToken(formula.substr(1, i - 1)) + getToken(formula.substr(i + 1));
}
private:
unordered_map<string, int> spreadsheet;
int getToken(const string& token) {
return isdigit(token[0])
? stoi(token)
: (spreadsheet.contains(token) ? spreadsheet[token] : 0);
}
};
/* code provided by PROGIEZ */
3484. Design Spreadsheet LeetCode Solution in Java
class Spreadsheet {
public Spreadsheet(int rows) {}
public void setCell(String cell, int value) {
spreadsheet.put(cell, value);
}
public void resetCell(String cell) {
spreadsheet.put(cell, 0);
}
public int getValue(String formula) {
final int i = formula.indexOf('+');
return getToken(formula.substring(1, i)) + getToken(formula.substring(i + 1));
}
private Map<String, Integer> spreadsheet = new HashMap<>();
private int getToken(final String token) {
return Character.isDigit(token.charAt(0)) ? Integer.parseInt(token)
: spreadsheet.getOrDefault(token, 0);
}
}
// code provided by PROGIEZ
3484. Design Spreadsheet LeetCode Solution in Python
class Spreadsheet:
def __init__(self, rows: int) -> None:
self.spreadsheet = {}
def setCell(self, cell: str, value: int) -> None:
self.spreadsheet[cell] = value
def resetCell(self, cell: str) -> None:
self.spreadsheet[cell] = 0
def getValue(self, formula: str) -> int:
i = formula.find('+')
return self._getToken(formula[1:i]) + self._getToken(formula[i+1:])
def _getToken(self, token: str) -> int:
return int(token) if token[0].isdigit() else self.spreadsheet.get(token, 0)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.