Skip to content

Commit 3649927

Browse files
committed
leetcode
1 parent da8c50d commit 3649927

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
3+
4+
-* Excel Sheet Column Number *-
5+
6+
7+
8+
Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.
9+
10+
For example:
11+
12+
A -> 1
13+
B -> 2
14+
C -> 3
15+
...
16+
Z -> 26
17+
AA -> 27
18+
AB -> 28
19+
...
20+
21+
22+
Example 1:
23+
24+
Input: columnTitle = "A"
25+
Output: 1
26+
Example 2:
27+
28+
Input: columnTitle = "AB"
29+
Output: 28
30+
Example 3:
31+
32+
Input: columnTitle = "ZY"
33+
Output: 701
34+
35+
36+
Constraints:
37+
38+
1 <= columnTitle.length <= 7
39+
columnTitle consists only of uppercase English letters.
40+
columnTitle is in the range ["A", "FXSHRXW"].
41+
42+
*/
43+
44+
// extension Minus on String {
45+
// String operator -(String rhs) => replaceAll(rhs, '');
46+
// }
47+
48+
import 'dart:math';
49+
50+
class Solution {
51+
// Time Limit exceeds
52+
int titleToNumber(String columnTitle) {
53+
int sum = 0;
54+
for (String ch in columnTitle.split("")) {
55+
int diff = ch.codeUnitAt(0) - ('A'.codeUnitAt(0) - 1);
56+
sum = sum * 26 + diff;
57+
}
58+
return sum;
59+
}
60+
}
61+
62+
class B {
63+
// Runtime: 656 ms, faster than 60.00% of Dart online submissions for Excel Sheet Column Number.
64+
// Memory Usage: 158.5 MB, less than 40.00% of Dart online submissions for Excel Sheet Column Number.
65+
66+
int titleToNumber(String columnTitle) {
67+
// Initialize the output result...
68+
int result = 0;
69+
// Traverse the whole list through the loop...
70+
for (int i = 0; i < columnTitle.length; i++) {
71+
// Process to get the excel sheet column number...
72+
result *= 26;
73+
result += columnTitle[i].codeUnitAt(0) - 'A'.codeUnitAt(0) + 1;
74+
}
75+
return result;
76+
}
77+
}
78+
79+
class C {
80+
// Runtime: 693 ms, faster than 40.00% of Dart online submissions for Excel Sheet Column Number.
81+
// Memory Usage: 148.3 MB, less than 60.00% of Dart online submissions for Excel Sheet Column Number.
82+
83+
int titleToNumber(String columnTitle) {
84+
return columnTitle.length == 0
85+
? 0
86+
: (columnTitle.codeUnitAt(columnTitle.length - 1) -
87+
'A'.codeUnitAt(0) +
88+
1) +
89+
26 *
90+
titleToNumber(columnTitle.substring(0, columnTitle.length - 1));
91+
}
92+
}
93+
94+
class D {
95+
/*
96+
Idea:
97+
Similar to our decimal representation on base 10, the excel column title also has similar representation but with base 26.
98+
99+
In deciaml representation, for example, 123 would mean 1x10^2 + 2x10^1 + 3x10^0
100+
In excel column title, for example, ABC would mean 1x26^2 + 2x26^1 + 3x26^0
101+
102+
We can use charCodeAt to convert letter to number, but it is offset by 64, so we need to subtract 64.
103+
Then we can use the representation from above to find the sum
104+
105+
*/
106+
// Time lImit Exceed
107+
int titleToNumber(String columnTitle) {
108+
int sum = 0;
109+
for (int i = 0; i < columnTitle.length; i++) {
110+
int letter2Num = columnTitle.codeUnitAt(i) - 64;
111+
sum = (sum + pow(26, columnTitle.length - 1 - i) * letter2Num).toInt();
112+
}
113+
114+
return sum;
115+
}
116+
}
117+
118+
class E {
119+
// Runtime: 726 ms, faster than 20.00% of Dart online submissions for Excel Sheet Column Number.
120+
// Memory Usage: 148.7 MB, less than 60.00% of Dart online submissions for Excel Sheet Column Number.
121+
int titleToNumber(String columnTitle) {
122+
int ans = 0;
123+
124+
for (int i = 0; i < columnTitle.length; i++) {
125+
ans *= 26;
126+
int c = columnTitle.codeUnitAt(i);
127+
int n = (c - 'A'.codeUnitAt(0) + 1);
128+
ans += n;
129+
}
130+
return ans;
131+
}
132+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# 🔥 Excel Sheet Column Number 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Working Solutions
4+
5+
### Solution - 1
6+
7+
```dart
8+
class Solution {
9+
// Runtime: 656 ms, faster than 60.00% of Dart online submissions for Excel Sheet Column Number.
10+
// Memory Usage: 158.5 MB, less than 40.00% of Dart online submissions for Excel Sheet Column Number.
11+
12+
int titleToNumber(String columnTitle) {
13+
// Initialize the output result...
14+
int result = 0;
15+
// Traverse the whole list through the loop...
16+
for (int i = 0; i < columnTitle.length; i++) {
17+
// Process to get the excel sheet column number...
18+
result *= 26;
19+
result += columnTitle[i].codeUnitAt(0) - 'A'.codeUnitAt(0) + 1;
20+
}
21+
return result;
22+
}
23+
}
24+
```
25+
26+
### Solution - 2
27+
28+
```dart
29+
class Solution {
30+
// Runtime: 693 ms, faster than 40.00% of Dart online submissions for Excel Sheet Column Number.
31+
// Memory Usage: 148.3 MB, less than 60.00% of Dart online submissions for Excel Sheet Column Number.
32+
33+
int titleToNumber(String columnTitle) {
34+
return columnTitle.length == 0
35+
? 0
36+
: (columnTitle.codeUnitAt(columnTitle.length - 1) -
37+
'A'.codeUnitAt(0) +
38+
1) +
39+
26 *
40+
titleToNumber(columnTitle.substring(0, columnTitle.length - 1));
41+
}
42+
}
43+
```
44+
45+
### Solution - 3
46+
47+
```dart
48+
class Solution {
49+
// Runtime: 726 ms, faster than 20.00% of Dart online submissions for Excel Sheet Column Number.
50+
// Memory Usage: 148.7 MB, less than 60.00% of Dart online submissions for Excel Sheet Column Number.
51+
int titleToNumber(String columnTitle) {
52+
int ans = 0;
53+
54+
for (int i = 0; i < columnTitle.length; i++) {
55+
ans *= 26;
56+
int c = columnTitle.codeUnitAt(i);
57+
int n = (c - 'A'.codeUnitAt(0) + 1);
58+
ans += n;
59+
}
60+
return ans;
61+
}
62+
}
63+
```
64+
65+
## Time Limit Exceeded
66+
67+
### Solution - 1 TLC
68+
69+
```dart
70+
class Solution {
71+
// Time Limit exceeds
72+
int titleToNumber(String columnTitle) {
73+
int sum = 0;
74+
for (String ch in columnTitle.split("")) {
75+
int diff = ch.codeUnitAt(0) - ('A'.codeUnitAt(0) - 1);
76+
sum = sum * 26 + diff;
77+
}
78+
return sum;
79+
}
80+
}
81+
```
82+
83+
### Solution - 2 TLC
84+
85+
Idea:
86+
Similar to our decimal representation on base 10, the excel column title also has similar representation but with base 26.
87+
88+
In decimal representation, for example, 123 would mean 1x10^2 + 2x10^1 + 3x10^0
89+
In excel column title, for example, ABC would mean 1x26^2 + 2x26^1 + 3x26^0
90+
91+
We can use charCodeAt to convert letter to number, but it is offset by 64, so we need to subtract 64.
92+
Then we can use the representation from above to find the sum
93+
94+
```dart
95+
class Solution {
96+
int titleToNumber(String columnTitle) {
97+
int sum = 0;
98+
for (int i = 0; i < columnTitle.length; i++) {
99+
int letter2Num = columnTitle.codeUnitAt(i) - 64;
100+
sum = (sum + pow(26, columnTitle.length - 1 - i) * letter2Num).toInt();
101+
}
102+
103+
return sum;
104+
}
105+
}
106+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"strings"
6+
)
7+
8+
func titleToNumber(columnTitle string) int {
9+
result := 0.0
10+
s := strings.ToLower(columnTitle)
11+
size := len(s)
12+
for i := 0; i < size; i++ {
13+
num := s[i] % 96
14+
n := size - 1 - i
15+
result += math.Pow(float64(26), float64(n)) * float64(num)
16+
}
17+
return int(result)
18+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
6363
- [Majority Element](MajorityElement/majority_element.dart)
6464
- [The Skyline Problem](TheSkylineProblem/the_skyline_problem.dart)
6565
- [Two Sum III - Data Structure Design](TwoSum-III-DataStructureDesign/two_sum_III_data_structure_design.dart)
66+
- [Excel Sheet Column Number](ExcelSheetColumnNumber/excel_sheet_colum_number.dart)
6667

6768
## Reach me via
6869

0 commit comments

Comments
 (0)