|
| 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