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
Constraints:
1 <= rows <= 1030 <= value <= 105"=X+Y", where X and Y are either valid cell references or non-negative integers with values less than or equal to 105.'A' to 'Z' followed by a row number between 1 and rows.104 calls will be made in total to setCell, resetCell, and getValue.class Spreadsheet {
int[][] cells;
public Spreadsheet(int rows) {
cells = new int[rows][27];
}
public void setCell(String cell, int value) {
int col = cell.charAt(0) - 'A';
int row = Integer.parseInt(cell.substring(1));
cells[row][col] = value;
}
public void resetCell(String cell) {
int col = cell.charAt(0) - 'A';
int row = Integer.parseInt(cell.substring(1));
cells[row][col] = 0;
}
public int getValue(String formula) {
formula = formula.substring(1);
String[] expr = formula.split("\\+");
int val1, val2;
if (Character.isAlphabetic((int) (expr[0].charAt(0)))) {
int col = expr[0].charAt(0) - 'A';
int row = Integer.parseInt(expr[0].substring(1));
val1 = cells[row][col];
} else
val1 = Integer.parseInt(expr[0]);
if (Character.isAlphabetic((int) (expr[1].charAt(0)))) {
int col = expr[1].charAt(0) - 'A';
int row = Integer.parseInt(expr[1].substring(1));
val2 = cells[row][col];
} else
val2 = Integer.parseInt(expr[1]);
return val1 + val2;
}
}
/**
* Your Spreadsheet object will be instantiated and called as such:
* Spreadsheet obj = new Spreadsheet(rows);
* obj.setCell(cell,value);
* obj.resetCell(cell);
* int param_3 = obj.getValue(formula);
*/