DSA Tracker

Visualize

Excel Sheet Column Number

Base-26, but 1-indexed (A=1..Z=26)

time O(L)space O(1)

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

A
[0]
B
[1]

Process each character left to right, building result = result*26 + value.

  • ch=0· arr[0] = A
Pseudocode
1FUNCTION titleToNumber(s):
2 result = 0
3 FOR each character ch in s:
4 value = position of ch in the alphabet (A=1 .. Z=26)
5 result = result * 26 + value
6 RETURN result

← / → step · space play · Home restart

Every step in words

Show all 8 steps
  1. Process each character left to right, building result = result*26 + value.
  2. Excel columns are 1-indexed: A=1, B=2, ... Z=26 (not the usual 0-based digit value).
  3. char = 'A'. A is the 1st letter, value = 1.
  4. result = result*26 + value = 0*26 + 1 = 1.
  5. char = 'B'. B is the 2nd letter, value = 2.
  6. result = result*26 + value = 1*26 + 2 = 28.
  7. Both characters processed — no more left in s.
  8. Final result = 28.

Now solve it yourself

Watching is the first half. The second is writing it without the animation.