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 <= 7columnTitle consists only of uppercase English letters.columnTitle is in the range ["A", "FXSHRXW"].Problem Overview: Excel labels its columns using letters: A, B, ..., Z, AA, AB, and so on. Given a column title as a string, you need to convert it to the corresponding column number. The system behaves like a base‑26 number system where letters map to values 1–26.
Approach 1: Iterative Approach Using Base Conversion (Time: O(n), Space: O(1))
The column title works like a positional number system, similar to base‑26. Each character contributes to the final value based on its position. Iterate through the string from left to right. For every character, multiply the current result by 26 and add the numeric value of the character (char - 'A' + 1). For example, "AB" becomes (1 * 26) + 2 = 28. This mirrors how decimal numbers are built digit by digit.
This method uses simple arithmetic and a single pass through the string. No additional data structures are required, so the space complexity stays constant. The approach relies on basic math operations and efficient string traversal. Because it performs one pass through the input, the time complexity is O(n) where n is the length of the column title.
The key insight: treat the string as a base‑26 number where digits range from 1 to 26 instead of 0 to 25. Each iteration shifts the existing value left by multiplying by 26, then adds the new digit value.
Approach 2: Recursive Approach (Time: O(n), Space: O(n))
The recursive method applies the same base‑26 logic but processes the string by breaking it into smaller prefixes. For a string like "ABC", compute the value of the prefix "AB" recursively, multiply it by 26, then add the value of the last character. The recurrence becomes: value(s) = 26 * value(prefix) + last_char_value.
Each recursive call reduces the string size by one character until the base case (a single character) is reached. This produces the same numeric result as the iterative approach but builds it through the call stack. Because each character is processed once, the time complexity remains O(n). However, recursion introduces stack frames, so the space complexity becomes O(n).
This approach highlights the positional nature of the number system and can be easier to reason about conceptually. It also demonstrates how recursive decomposition works with recursion and string prefixes.
Recommended for interviews: The iterative base‑conversion approach is what most interviewers expect. It shows you recognize the mapping between Excel columns and a base‑26 numbering system and can implement it efficiently in O(n) time with O(1) space. The recursive version demonstrates conceptual understanding but is less practical due to stack overhead.
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.
C++
Java
Python
C#
JavaScript
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.
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n), due to recursion stack space.
| Approach | Complexity |
|---|---|
| Iterative Approach Using Base Conversion | Time Complexity: O(n), where n is the length of the column title. |
| Recursive Approach | Time Complexity: O(n) |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Base‑26 Conversion | O(n) | O(1) | Best general solution. Efficient and expected in most coding interviews. |
| Recursive Calculation | O(n) | O(n) | Useful for understanding the positional base‑26 relationship using recursion. |
Excel Sheet Column Number | LeetCode 171 | C++, Java, Python • Knowledge Center • 32,550 views views
Watch 9 more video solutions →Practice Excel Sheet Column Number with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor