Excel Sheet Column Number – Solution & Complexity
1. Understand the Pattern
- The title behaves like a base-26 number, but the digits run
1..26, not0..25. Acontributes1,Zcontributes26.- Reading left to right, each new letter shifts the running total up by a factor of 26.
2. Map a Letter to Its Value
- Subtract the code point of
Aand add1. - So
A -> 1,B -> 2, ...,Z -> 26. - This is the per-digit value for the base-26 sum.
3. Accumulate Left to Right
- Start the result at
0. - For each letter, multiply the result by
26and add the letter's value. - After the last letter the result is the column number.
4. Final Solution and Complexity
- Each character is visited once.
- Time complexity is
O(L)for a title of lengthL. - Space complexity is
O(1).