Skip to content

Commit 8100caf

Browse files
committed
O(n) time and O(1) space
1 parent 7cdf772 commit 8100caf

File tree

1 file changed

+39
-0
lines changed
  • 30 Days August Challange/Week 2/4. Excel Sheet Column Number

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Given a column title as appear in an Excel sheet, return its corresponding column number.
3+
4+
For example:
5+
6+
A -> 1
7+
B -> 2
8+
C -> 3
9+
...
10+
Z -> 26
11+
AA -> 27
12+
AB -> 28
13+
...
14+
Example 1:
15+
16+
Input: "A"
17+
Output: 1
18+
Example 2:
19+
20+
Input: "AB"
21+
Output: 28
22+
Example 3:
23+
24+
Input: "ZY"
25+
Output: 701
26+
27+
28+
Constraints:
29+
30+
1 <= s.length <= 7
31+
s consists only of uppercase English letters.
32+
s is between "A" and "FXSHRXW".
33+
"""
34+
class Solution:
35+
def titleToNumber(self, s: str) -> int:
36+
result = 0
37+
for i,char in enumerate(s[::-1]):
38+
result += (ord(char) - ord('A') + 1 ) * (26**i)
39+
return result

0 commit comments

Comments
 (0)