#3484
Medium Algorithms Design spreadsheet
Array Hash Table String Design Matrix
74.3% acceptance
Feb 24, 2026
325
30
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.
Solution
Rust
Time O(2^n)
Space O(n)
* impl Spreadsheet {
* fn new(rows: i32) -> Self {
* }
* fn set_cell(&self, cell: String, value: i32) {
* }
* fn reset_cell(&self, cell: String) {
* }
* fn get_value(&self, formula: String) -> i32 {
* }
* }
*/
/**
* Your Spreadsheet object will be instantiated and called as such:
* let obj = Spreadsheet::new(rows);
* obj.set_cell(cell, value);
* obj.reset_cell(cell);
* let ret_3: i32 = obj.get_value(formula);
*/
pub struct Spreadsheet {
data: std::collections::HashMap<String, i32>,
}
impl Spreadsheet {
pub fn new(_rows: i32) -> Self { Spreadsheet { data: std::collections::HashMap::new() } }
pub fn set_cell(&mut self, cell: String, value: i32) { self.data.insert(cell, value); }
pub fn reset_cell(&mut self, cell: String) { self.data.remove(&cell); }
pub fn get_value(&self, formula: String) -> i32 {
let expr = &formula[1..]; // skip '='
let parts: Vec<&str> = expr.split('+').collect();
let eval = |s: &str| -> i32 {
if s.chars().next().map(|c| c.is_ascii_uppercase()).unwrap_or(false) {
*self.data.get(s).unwrap_or(&0)
} else {
s.parse().unwrap_or(0)
}
};
eval(parts[0]) + eval(parts[1])
}
}