Skip to content

Commit 52d886b

Browse files
committed
2194. Cells in a Range on an Excel Sheet: AC
1 parent ad882a5 commit 52d886b

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,3 +1659,4 @@ mod s2190_most_frequent_number_following_key_in_an_array;
16591659
mod s2191_sort_the_jumbled_numbers;
16601660
mod s2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph;
16611661
mod s2193_minimum_number_of_moves_to_make_palindrome;
1662+
mod s2194_cells_in_a_range_on_an_excel_sheet;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* [2194] Cells in a Range on an Excel Sheet
3+
*
4+
* A cell (r, c) of an excel sheet is represented as a string "<col><row>" where:
5+
*
6+
* <col> denotes the column number c of the cell. It is represented by alphabetical letters.
7+
*
8+
* For example, the 1^st column is denoted by 'A', the 2^nd by 'B', the 3^rd by 'C', and so on.
9+
*
10+
*
11+
* <row> is the row number r of the cell. The r^th row is represented by the integer r.
12+
*
13+
* You are given a string s in the format "<col1><row1>:<col2><row2>", where <col1> represents the column c1, <row1> represents the row r1, <col2> represents the column c2, and <row2> represents the row r2, such that r1 <= r2 and c1 <= c2.
14+
* Return the list of cells (x, y) such that r1 <= x <= r2 and c1 <= y <= c2. The cells should be represented as strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows.
15+
*
16+
* Example 1:
17+
* <img alt="" src="https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png" style="width: 250px; height: 160px;" />
18+
* Input: s = "K1:L2"
19+
* Output: ["K1","K2","L1","L2"]
20+
* Explanation:
21+
* The above diagram shows the cells which should be present in the list.
22+
* The red arrows denote the order in which the cells should be presented.
23+
*
24+
* Example 2:
25+
* <img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png" style="width: 500px; height: 50px;" />
26+
* Input: s = "A1:F1"
27+
* Output: ["A1","B1","C1","D1","E1","F1"]
28+
* Explanation:
29+
* The above diagram shows the cells which should be present in the list.
30+
* The red arrow denotes the order in which the cells should be presented.
31+
*
32+
*
33+
* Constraints:
34+
*
35+
* s.length == 5
36+
* 'A' <= s[0] <= s[3] <= 'Z'
37+
* '1' <= s[1] <= s[4] <= '9'
38+
* s consists of uppercase English letters, digits and ':'.
39+
*
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/
44+
// discuss: https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn cells_in_range(s: String) -> Vec<String> {
50+
let s = s.as_bytes();
51+
(s[0]..=s[3])
52+
.flat_map(|col| (s[1]..=s[4]).map(move |row| format!("{}{}", col as char, row as char)))
53+
.collect::<Vec<_>>()
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_2194_example_1() {
65+
let s = "K1:L2".to_string();
66+
67+
let result = vec_string!["K1", "K2", "L1", "L2"];
68+
69+
assert_eq!(Solution::cells_in_range(s), result);
70+
}
71+
72+
#[test]
73+
fn test_2194_example_2() {
74+
let s = "A1:F1".to_string();
75+
76+
let result = vec_string!["A1", "B1", "C1", "D1", "E1", "F1"];
77+
78+
assert_eq!(Solution::cells_in_range(s), result);
79+
}
80+
}

0 commit comments

Comments
 (0)