Excel Sheet Column Number
Base-26, but 1-indexed (A=1..Z=26)
This is base-26 arithmetic, but Excel columns are 1-indexed (A=1, not 0) — so each character contributes result*26 + (its 1-based letter value) instead of the usual 0-based digit value.
Details
Animated on: s = "AB" — convert the column title to its column number.
Problem:Excel Sheet Column NumberEasy
Base-26, but 1-indexed (A=1..Z=26)
step 1 / 8
Process each character left to right, building result = result*26 + value.
- ch=0· arr[0] = A
1FUNCTION titleToNumber(s):2 result = 03 FOR each character ch in s:4 value = position of ch in the alphabet (A=1 .. Z=26)5 result = result * 26 + value6 RETURN result
← / → step · space play · Home restart
Every step in words
Show all 8 steps
- Process each character left to right, building result = result*26 + value.
- Excel columns are 1-indexed: A=1, B=2, ... Z=26 (not the usual 0-based digit value).
- char = 'A'. A is the 1st letter, value = 1.
- result = result*26 + value = 0*26 + 1 = 1.
- char = 'B'. B is the 2nd letter, value = 2.
- result = result*26 + value = 1*26 + 2 = 28.
- Both characters processed — no more left in s.
- Final result = 28.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.