Skip to main content

Excel Sheet Column Number - Solution & Explanation

EasyMathString13 min readAsked at: Amazon, Microsoft, Goldman Sachs +6
Practice this problem

Problem Statement

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

 

Example 1:

Input: columnTitle = "A"
Output: 1

Example 2:

Input: columnTitle = "AB"
Output: 28

Example 3:

Input: columnTitle = "ZY"
Output: 701

 

Constraints:

  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].

Approach Overview

Problem Overview: Excel labels columns using letters: A, B, ..., Z, AA, AB, and so on. Given a column title string, convert it to its corresponding numeric index. The mapping behaves like a modified base‑26 number system where A=1, B=2, ..., Z=26.

Approach 1: Iterative Approach Using Base Conversion (O(n) time, O(1) space)

This problem is essentially base conversion. Treat the column title as a number written in base 26, but with digits ranging from 1 to 26 instead of 0 to 25. Iterate through each character from left to right. For every character, multiply the current result by 26 and add the value of the current letter (char - 'A' + 1). This mirrors how decimal numbers are built digit by digit: result = result * 26 + value. The algorithm performs a single pass through the string, making it efficient and easy to implement in any language. Since only a running integer is maintained, the space usage stays constant. This approach relies on simple arithmetic and character manipulation from math and string fundamentals.

Example: for "AB", start with result = 0. Process 'A': 0 * 26 + 1 = 1. Process 'B': 1 * 26 + 2 = 28. The final result is 28. The same logic scales for longer titles like "FXSHRXW" without any additional data structures.

Approach 2: Recursive Approach (O(n) time, O(n) recursion space)

The same base‑26 logic can be expressed recursively. Process the string from left to right by splitting it into two parts: the prefix and the last character. Recursively compute the numeric value of the prefix, multiply it by 26, and then add the value of the final character. Each recursive call reduces the string size by one until the base case (single character) is reached. The formula becomes: value(prefix) * 26 + value(last_char).

This version mirrors how positional number systems work conceptually, making it useful for explaining the idea in interviews or teaching recursion. However, it introduces call stack overhead proportional to the string length, so its auxiliary space complexity becomes O(n). For production or competitive coding, the iterative version is typically preferred because it avoids recursion overhead while producing the same result.

Recommended for interviews: The iterative base‑conversion approach is what interviewers expect. It demonstrates that you recognize the Excel column naming scheme as a positional base‑26 system and can translate that insight into a simple loop. Mentioning the recursive formulation shows deeper understanding, but implementing the iterative solution quickly and correctly is usually the strongest signal.

Approach 1: Iterative Approach Using Base Conversion

This approach involves treating each character of the column title string as a digit in a base-26 number system. We iterate over the string from left to right, calculating its contribution to the overall number at each step by multiplying with an appropriate power of 26.

We initialize a result variable to 0. As we iterate over each character of the column title, we multiply the current result by 26 and add the integer value of the current character (adjusted by 'A' to get 1-based index). This mimics adding a digit in a base-26 system.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the column title.
Space Complexity: O(1), as no additional space is used that scales with input size.

Try this approach in the editor →

Approach 2: Recursive Approach

This approach uses recursion to compute the column number. Starting from the first character, the function recursively converts the rest of the string and adds the current character's contribution by treating it as a digit in a 26-based number system. This implements a divide-and-conquer strategy.

This recursive solution defines a helper function that processes the current character and recursively calls itself to handle the rest of the string. The base case is when the end of the string is reached.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n), due to recursion stack space.

Try this approach in the editor →

Approach 3: Base Conversion

The column name in Excel is a representation in base 26. For example, "AB" represents the column number 1 times 26 + 2 = 28.

Therefore, we can iterate through the string columnTitle, convert each character to its corresponding value, and then calculate the result.

The time complexity is O(n), where n is the length of the string columnTitle. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach Using Base Conversion

Time Complexity: O(n), where n is the length of the column title.
Space Complexity: O(1), as no additional space is used that scales with input size.

Recursive Approach

Time Complexity: O(n)
Space Complexity: O(n), due to recursion stack space.

Base Conversion

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Base ConversionO(n)O(1)Best general solution; minimal memory and simplest implementation
Recursive ApproachO(n)O(n)Useful for explaining positional base‑26 logic recursively

Video Solution

Excel Sheet Column Number | LeetCode 171 | C++, Java, PythonKnowledge Center33,806 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Excel Sheet Column Number easy or hard?
Excel Sheet Column Number is categorized as an Easy problem on LeetCode with an acceptance rate around 67%. The main challenge is recognizing the base‑26 pattern behind Excel column naming rather than implementing complex algorithms.
Excel Sheet Column Number Python/Java solution
The typical Python or Java solution loops through the string and updates a running integer using result = result * 26 + (c - 'A' + 1). This compact logic works identically across languages such as Python, Java, C++, JavaScript, and C# with O(n) time complexity.
How to solve Excel Sheet Column Number in O(n)?
Use base‑26 positional conversion. Initialize a result variable to 0, iterate through each character, multiply the current result by 26, and add the letter value (A=1 to Z=26). Since each character is processed once, the algorithm runs in O(n) time with constant extra space.
What is the best approach for Excel Sheet Column Number?
The best approach is treating the column title as a base‑26 number and converting it iteratively. Traverse the string from left to right and compute result = result * 26 + (character value). This runs in O(n) time and O(1) space, making it the most efficient and commonly expected interview solution.
Is Excel Sheet Column Number asked at Google/Amazon/Meta?
Excel Sheet Column Number is a common easy-level interview problem that tests understanding of positional number systems and string processing. Variants of this problem appear in interviews at companies like Amazon, Google, and Microsoft as warm‑up or screening questions.
What data structure is used in Excel Sheet Column Number?
No special data structure is required. The solution primarily uses simple arithmetic operations and string traversal. The key concept comes from math—interpreting the string as a base‑26 number with characters mapped to numeric values.
What is the time complexity of Excel Sheet Column Number?
The time complexity is O(n), where n is the length of the column title string. Each character is processed exactly once to update the running numeric value. Space complexity is O(1) for the iterative solution and O(n) for the recursive version due to the call stack.

Ready to solve this problem?

Practice Excel Sheet Column Number with our built-in code editor and test cases.

Practice on FleetCode