Post

3797 Design Spreadsheet

3797 Design Spreadsheet

Design Spreadsheet image

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:

1
2
3
4
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
2
3
4
5
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

struct Spreadsheet {
    arr : Vec<Vec<i32>>
}


/** 
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl Spreadsheet {

    fn new(rows: i32) -> Self {
        let mut arr : Vec<Vec<i32>> = Vec::new();
        for i in 0..rows {
            let mut tmp : Vec<i32> = Vec::new();
            for j in 0..26 {
                tmp.push(0);
            }
            arr.push(tmp);
        }
        let mut spreadSheet = Spreadsheet{
            arr : arr
        };
        return spreadSheet

    }
    
    fn set_cell(&mut self, cell: String, value: i32) {
    // parse as Excel style: Letter = column, Number = 1-based row
    let mut chars = cell.chars();
    let col_char = chars.next().unwrap().to_ascii_lowercase();
    let row_str: String = chars.collect();
    let row: usize = row_str.parse::<usize>().unwrap() - 1;
    let col = (col_char as u8 - b'a') as usize;

    self.arr[row][col] = value;
}

fn reset_cell(&mut self, cell: String) {
    // same parsing as set_cell
    let mut chars = cell.chars();
    let col_char = chars.next().unwrap().to_ascii_lowercase();
    let row_str: String = chars.collect();
    let row: usize = row_str.parse::<usize>().unwrap() - 1;
    let col = (col_char as u8 - b'a') as usize;

    self.arr[row][col] = 0;
}

    
    fn get_value(&self, formula: String) -> i32 {
    let args = formula.replace("=", "");
    let str_arr = args.split('+');
    let mut result = 0;

    for k in str_arr {
        println!("{:?}", k);

        if let Some(first) = k.chars().next() {
            if first.is_ascii_alphabetic() {
                // ✅ Only treat as cell if first char is a letter
                let mut chars = k.chars();
                let first = chars.next().unwrap().to_ascii_lowercase();
                let col_str: String = chars.collect();
                let row: usize = col_str.parse().unwrap();

                println!("{:?} --- {:?}", first, row);

                if ('a'..='z').contains(&first) {
                    let col = (first as u8 - b'a') as usize;
                    result += self.arr[row - 1][col]; // rows are 1-indexed
                }
                continue;
            }
        }

        // otherwise parse as number
        if let Ok(num) = k.parse::<i32>() {
            result += num;
        }
    }

    result
}

}


/**
 * 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);
 */



This post is licensed under CC BY 4.0 by the author.