-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2194-CellsInARangeOnAnExcelSheet.go
More file actions
72 lines (63 loc) · 2.9 KB
/
2194-CellsInARangeOnAnExcelSheet.go
File metadata and controls
72 lines (63 loc) · 2.9 KB
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
package main
// 2194. Cells in a Range on an Excel Sheet
// A cell (r, c) of an excel sheet is represented as a string "<col><row>" where:
// <col> denotes the column number c of the cell. It is represented by alphabetical letters.
// For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.
// <row> is the row number r of the cell. The rth row is represented by the integer r.
// 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.
// 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.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png" />
// Input: s = "K1:L2"
// Output: ["K1","K2","L1","L2"]
// Explanation:
// The above diagram shows the cells which should be present in the list.
// The red arrows denote the order in which the cells should be presented.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png" />
// Input: s = "A1:F1"
// Output: ["A1","B1","C1","D1","E1","F1"]
// Explanation:
// The above diagram shows the cells which should be present in the list.
// The red arrow denotes the order in which the cells should be presented.
// Constraints:
// s.length == 5
// 'A' <= s[0] <= s[3] <= 'Z'
// '1' <= s[1] <= s[4] <= '9'
// s consists of uppercase English letters, digits and ':'.
import "fmt"
import "strings"
func cellsInRange(s string) []string {
c := strings.SplitN(s, ":", 2) // Scan given input
c1, r1 := c[0][0], c[0][1]
c2, r2 := c[1][0], c[1][1]
res := []string{}
for i := c1; i <= c2; i++ { // Generate rows and colums in between
for j := r1; j <= r2; j++ {
res = append(res, fmt.Sprintf("%c%c", i, j))
}
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png" />
// Input: s = "K1:L2"
// Output: ["K1","K2","L1","L2"]
// Explanation:
// The above diagram shows the cells which should be present in the list.
// The red arrows denote the order in which the cells should be presented.
fmt.Println(cellsInRange("K1:L2")) // ["K1","K2","L1","L2"]
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png" />
// Input: s = "A1:F1"
// Output: ["A1","B1","C1","D1","E1","F1"]
// Explanation:
// The above diagram shows the cells which should be present in the list.
// The red arrow denotes the order in which the cells should be presented.
fmt.Println(cellsInRange("A1:F1")) // ["A1","B1","C1","D1","E1","F1"]
}