-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexcel_sheet_column_title.dart
136 lines (110 loc) · 3.45 KB
/
excel_sheet_column_title.dart
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
-* Excel Sheet Column Title *-
Given an integer columnNumber, return its corresponding
column title as it appears in an
Excel sheet.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: columnNumber = 1
Output: "A"
Example 2:
Input: columnNumber = 28
Output: "AB"
Example 3:
Input: columnNumber = 701
Output: "ZY"
Constraints:
1 <= columnNumber <= 231 - 1
*/
class A {
/**
* Time Complexity: O(log26n), as we are using a loop and in each traversal, we decrement by floor division of 26.
Auxiliary Space: O(10000), as we are using extra space for the array.
*/
// Runtime: 512 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
// Memory Usage: 141.5 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
String convertToTitle(int columnNumber) {
List<int> arr = List.filled(10000, 0);
int i = 0;
// Step 1: Converting to number assuming
// 0 in number system
while (columnNumber > 0) {
arr[i] = columnNumber % 26;
columnNumber = columnNumber ~/ 26;
i++;
}
// Step 2: Getting rid of 0, as 0 is
// not part of number system
for (int j = 0; j < i - 1; j++) {
if (arr[j] <= 0) {
arr[j] += 26;
arr[j + 1] = arr[j + 1] - 1;
}
}
String ans = '';
for (int j = i; j >= 0; j--) {
if (arr[j] > 0) ans += String.fromCharCode(65 + arr[j] - 1);
}
return ans;
}
}
class B {
// Runtime: 391 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
// Memory Usage: 140.1 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
String convertToTitle(int columnNumber) {
String alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (columnNumber < 26)
return alpha[columnNumber - 1];
else {
int q = (columnNumber ~/ 26), r = columnNumber % 26;
if (r == 0) {
if (q == 1)
return alpha[(26 + r - 1)];
else
return convertToTitle(q - 1) + alpha[(26 + r - 1)];
} else
return convertToTitle(q) + alpha[r - 1];
}
}
}
class C {
// Runtime: 490 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
// Memory Usage: 150.5 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
String convertToTitle(int columnNumber) {
if (columnNumber-- == 0) return "";
return convertToTitle(columnNumber ~/ 26) +
String.fromCharCode((columnNumber % 26) + 'A'.codeUnitAt(0));
}
}
class D {
// Runtime: 305 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
// Memory Usage: 157.9 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
String convertToTitle(int columnNumber) {
StringBuffer result = StringBuffer();
while (columnNumber > 0) {
columnNumber--;
//result.write(0, ('A' + n % 26));
result.writeCharCode(((columnNumber % 26) + 'A'.codeUnitAt(0)));
columnNumber ~/= 26;
}
return result.toString().split("").reversed.join("");
}
}
class E {
// Runtime: 324 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
// Memory Usage: 140 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
String convertToTitle(int columnNumber) {
return columnNumber == 0
? ""
: convertToTitle(--columnNumber ~/ 26) +
String.fromCharCode((columnNumber % 26) + 'A'.codeUnitAt(0));
}
}