Design Spreadsheet

medium hash table string design matrix

Problem

A spreadsheet has 26 columns ('A''Z') and rows rows; every cell starts at 0. Implement setCell("AX", value), resetCell("AX") (back to 0), and getValue("=X+Y") where each of X, Y is a cell reference or a non-negative integer. An unset cell reads as 0.

InputgetValue("=5+7"), setCell("A1",10), getValue("=A1+6"), setCell("B2",15), getValue("=A1+B2"), resetCell("A1"), getValue("=A1+B2")
Output12, 16, 25, 15
A1=10 then B2=15 give 25; after resetCell("A1") the last formula is 0+15 = 15.

class Spreadsheet:
    def __init__(self, rows):
        self.cells = {}                 # cell name -> value, default 0

    def setCell(self, cell, value):
        self.cells[cell] = value

    def resetCell(self, cell):
        self.cells[cell] = 0

    def _operand(self, tok):            # cell ref or literal int
        if tok[0].isalpha():
            return self.cells.get(tok, 0)
        return int(tok)

    def getValue(self, formula):
        x, y = formula[1:].split("+")   # drop '=', split on '+'
        return self._operand(x) + self._operand(y)
class Spreadsheet {
  constructor(rows) {
    this.cells = new Map();              // cell name -> value, default 0
  }
  setCell(cell, value) {
    this.cells.set(cell, value);
  }
  resetCell(cell) {
    this.cells.set(cell, 0);
  }
  operand(tok) {                         // cell ref or literal int
    if (tok[0] >= "A" && tok[0] <= "Z")
      return this.cells.get(tok) ?? 0;
    return parseInt(tok, 10);
  }
  getValue(formula) {
    const [x, y] = formula.slice(1).split("+"); // drop '=', split on '+'
    return this.operand(x) + this.operand(y);
  }
}
class Spreadsheet {
    Map<String, Integer> cells = new HashMap<>(); // default 0
    public Spreadsheet(int rows) { }

    public void setCell(String cell, int value) {
        cells.put(cell, value);
    }
    public void resetCell(String cell) {
        cells.put(cell, 0);
    }
    private int operand(String tok) {            // cell ref or literal
        if (Character.isLetter(tok.charAt(0)))
            return cells.getOrDefault(tok, 0);
        return Integer.parseInt(tok);
    }
    public int getValue(String formula) {
        String[] p = formula.substring(1).split("\\+"); // drop '='
        return operand(p[0]) + operand(p[1]);
    }
}
class Spreadsheet {
    unordered_map<string, int> cells;        // default 0
    int operand(const string& t) {           // cell ref or literal
        if (isalpha(t[0])) {
            auto it = cells.find(t);
            return it == cells.end() ? 0 : it->second;
        }
        return stoi(t);
    }
public:
    Spreadsheet(int rows) { }
    void setCell(string cell, int value) { cells[cell] = value; }
    void resetCell(string cell) { cells[cell] = 0; }
    int getValue(string formula) {
        int plus = formula.find('+');        // formula is "=X+Y"
        string x = formula.substr(1, plus - 1);
        string y = formula.substr(plus + 1);
        return operand(x) + operand(y);
    }
};
Time: O(1) per operation Space: O(cells written)