const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Excel Sheet Column Number: LeetCode 171 Solution

Excel Sheet Column Number (#171) is a base-26 conversion problem — it teaches number system conversion, the same concept behind encoding and decoding problems you will see in system design interviews.

5 min read|

Base-26 conversion — the same math behind URL shorteners

Excel Sheet Column Number #171 — decode column titles using the pattern that powers encoding everywhere

Excel Sheet Column Number Is Base-26 Math

Excel Sheet Column Number (#171) is one of those LeetCode problems that looks trivial until you try to explain the math behind it. You are given a column title like "A", "Z", or "AB" and asked to return the corresponding column number. The answer is pure number system conversion — the same principle behind binary, hexadecimal, and every other positional number system.

If you have ever wondered how Excel columns work past "Z", this problem makes it click. "A" is 1, "Z" is 26, "AA" is 27, and "AZ" is 52. The pattern is a 1-indexed base-26 system, and the excel sheet column number leetcode problem asks you to decode it into a regular integer.

Understanding this problem unlocks a broader pattern. Base conversion appears in URL shorteners (base-62), IP address encoding, and hash function design. LeetCode 171 is the gentlest introduction to that family of problems.

Understanding the Column Title to Number Problem

The problem statement is straightforward. Given a string columnTitle that represents the column title as it appears in an Excel sheet, return its corresponding column number. "A" maps to 1, "B" to 2, all the way through "Z" which maps to 26.

Where it gets interesting is multi-character titles. "AA" is 27, "AB" is 28, "AZ" is 52, "BA" is 53, and "ZZ" is 702. Each position represents a power of 26, just like each digit in a decimal number represents a power of 10.

The key insight is that this is a positional number system. The rightmost character is the "ones" place (26^0), the next character left is the "twenty-sixes" place (26^1), and so on. The only twist compared to standard base-26 is that digits range from 1 to 26 instead of 0 to 25.

The Base-26 Conversion Approach

The approach is identical to how you would convert a binary string to a decimal number, except the base is 26 instead of 2. You process the string from left to right, and for each character you multiply your running total by 26 and add the value of the current letter.

The value of each letter is its position in the alphabet: A = 1, B = 2, ..., Z = 26. In code, you compute this as charCode - 64 (since "A" has ASCII code 65) or equivalently char - "A" + 1.

The formula for the entire string is: result = result * 26 + (current character value). Start with result = 0, and after processing every character from left to right, result holds the final column number. That is the entire algorithm.

💡

Pro Tip

Think of it exactly like binary-to-decimal conversion, but replace base 2 with base 26 and '0'/'1' with 'A'-'Z'. result = result * 26 + (char - 'A' + 1).

Implementation of the Excel Column Number Solution

The implementation is remarkably concise. Initialize a variable result to 0. Loop through each character in the string. For each character, multiply result by 26 and add the character value (char code minus 64, or char minus "A" plus 1). Return result when the loop finishes.

In Python: result = 0, then for each char in columnTitle, result = result * 26 + (ord(char) - ord("A") + 1). In JavaScript or TypeScript: result = result * 26 + (columnTitle.charCodeAt(i) - 64). The logic is identical across languages.

The time complexity is O(n) where n is the length of the column title string. The space complexity is O(1) — you only use a single integer variable to accumulate the result. This leetcode 171 solution is optimal since you must read every character at least once.

  • Time complexity: O(n) — one pass through the column title string
  • Space complexity: O(1) — only a single accumulator variable
  • Process characters left to right: result = result * 26 + letter_value
  • Letter value: A=1, B=2, ..., Z=26 (charCode - 64 or char - "A" + 1)

Visual Walkthrough of Excel Column Conversion

Walking through examples makes the base 26 conversion intuitive. Let us trace through "AB" step by step. Start with result = 0.

Process "A": result = 0 * 26 + 1 = 1. Process "B": result = 1 * 26 + 2 = 28. The answer is 28. You can verify: column A is 1, so column AA is 27 (one full cycle of 26 plus 1), and column AB is 28.

Now trace "ZY". Process "Z": result = 0 * 26 + 26 = 26. Process "Y": result = 26 * 26 + 25 = 701. The answer is 701. This matches the formula: 26 * 26 + 25 = 676 + 25 = 701.

For a three-letter example, trace "AAA". Process first "A": result = 0 * 26 + 1 = 1. Process second "A": result = 1 * 26 + 1 = 27. Process third "A": result = 27 * 26 + 1 = 703. Every additional letter multiplies the accumulated value by 26 before adding the new digit.

  1. 1"AB": result = 0 -> (0*26 + 1) = 1 -> (1*26 + 2) = 28
  2. 2"ZY": result = 0 -> (0*26 + 26) = 26 -> (26*26 + 25) = 701
  3. 3"AAA": result = 0 -> 1 -> 27 -> 703
  4. 4"Z": result = 0 -> (0*26 + 26) = 26
  5. 5"AZ": result = 0 -> 1 -> (1*26 + 26) = 52
⚠️

Watch Out

This is 1-indexed base 26, not 0-indexed — 'A' = 1 (not 0), which means there's no 'zero' digit. This subtle difference from standard base conversion trips up some candidates.

Edge Cases for Excel Sheet Column Number

The simplest edge cases are single-letter inputs. "A" returns 1 and "Z" returns 26. These are your sanity checks — if your formula does not produce 1 for "A" and 26 for "Z", something is wrong with your character-to-value mapping.

Two-letter inputs test that your multiplication logic is correct. "AA" should return 27 (1 * 26 + 1), not 2 or 26. "AZ" returns 52 (1 * 26 + 26). "BA" returns 53 (2 * 26 + 1). If you get 26 for "AA", you are likely treating "A" as 0 instead of 1.

Three-letter inputs like "AAA" = 703 confirm that your solution handles arbitrary-length strings correctly. The LeetCode constraints guarantee the input is between "A" and "FXSHRXW" (2^31 - 1), so your solution must handle strings up to 7 characters long without integer overflow issues.

  • Single letter: "A" = 1, "Z" = 26 — verify your character mapping
  • Two letters: "AA" = 27, "AZ" = 52, "ZZ" = 702 — verify multiplication logic
  • Three letters: "AAA" = 703 — verify multi-position accumulation
  • Max input: "FXSHRXW" = 2,147,483,647 (2^31 - 1) — fits in a 32-bit integer

What Excel Column Number Teaches About Base Conversion

LeetCode 171 is the friendliest introduction to base conversion problems. The core pattern — process digits positionally and accumulate using multiply-then-add — is the exact same technique used in binary-to-decimal, hex-to-decimal, and any other base conversion you will encounter.

This pattern extends directly to system design problems. URL shorteners use base-62 encoding (a-z, A-Z, 0-9) to convert database IDs into short strings. The decoding step is precisely the same algorithm: process each character, multiply the running total by the base, and add the character value. If you can solve excel column explained problems, you can implement a URL shortener decoder.

The reverse problem — LeetCode 168, Excel Sheet Column Title — asks you to convert a number back to a column title. It is the encoding counterpart to this decoding problem, and together they form a complete round-trip conversion. Practice both directions to solidify the base conversion pattern, then review with YeetCode flashcards to lock it into long-term memory.

ℹ️

Why This Matters

Excel Column Number (#171) teaches base conversion — the same principle behind base-62 encoding used in URL shorteners, a common system design building block.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now