Excel Sheet Column Number solution and explanation
9 more videos on this problem
Excel Sheet Column Number | LeetCode 171 | C++, Java, PythonKnowledge Center · 7:07 · 33K views
Excel Sheet Column Number | Leetcode 171 | Day-22 | StringAyushi Sharma · 8:19 · 20K views
Excel Sheet Column Number | Leet code 171 | Theory explained + Python codeSai Anish Malla · 7:15 · 7K views
LeetCode Problem: 171. Excel Sheet Column Number | Java SolutionCode for Interview · 15:23 · 4K views
171. Excel Sheet Column Number || Java || Leetcode || HindiCoding Sphere · 6:34 · 4K views
171. Excel Sheet Column Number | LEETCODE EASY | MATHScode Explainer · 7:11 · 598 views
Leetcode 171. Excel Sheet Column Number (Solution + Explanation)ultcode · 5:10 · 323 views
Excel sheet column number | Leetcode 171 | Java solution explained with intuitionJoey'sTech · 12:36 · 284 views
171. Excel Sheet Column Number - JavaScript - One LineAlgoPrepMaster · 1:00 · 223 views
Daily Leetcode #693: Feb 22, 2022 - 171. Excel Sheet Column NumberProgramming Live with Larry · 6:45 · 186 views
The problem
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 <= 7columnTitleconsists only of uppercase English letters.columnTitleis 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.
def titleToNumber(columnTitle: str) -> int:
result = 0
for c in columnTitle:
result = result * 26 + (ord(c) - ord('A') + 1)
return result
# Example usage
title = "ZY"
print(titleToNumber(title))class Solution {
public int titleToNumber(String columnTitle) {
int result = 0;
for (char c : columnTitle.toCharArray()) {
result = result * 26 + (c - 'A' + 1);
}
return result;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.titleToNumber("ZY"));
}
}#include <iostream>
#include <string>
int titleToNumber(const std::string& columnTitle) {
int result = 0;
for (char c : columnTitle) {
result = result * 26 + (c - 'A' + 1);
}
return result;
}
int main() {
std::cout << titleToNumber("ZY") << std::endl;
return 0;
}function titleToNumber(columnTitle) {
let result = 0;
for (let i = 0; i < columnTitle.length; i++) {
result = result * 26 + (columnTitle.charCodeAt(i) - 'A'.charCodeAt(0) + 1);
}
return result;
}
// Example Usage
console.log(titleToNumber("ZY"));#include <stdio.h>
int titleToNumber(char * columnTitle) {
int result = 0;
while (*columnTitle) {
result = result * 26 + (*columnTitle - 'A' + 1);
columnTitle++;
}
return result;
}
int main() {
char columnTitle[] = "AB";
printf("%d\n", titleToNumber(columnTitle));
return 0;
}using System;
class Solution {
public int TitleToNumber(string columnTitle) {
int result = 0;
foreach (char c in columnTitle) {
result = result * 26 + (c - 'A' + 1);
}
return result;
}
static void Main() {
Solution sol = new Solution();
Console.WriteLine(sol.TitleToNumber("ZY"));
}
}The Python solution leverages the built-in ord() function to convert characters to numbers. We iterate through each character and apply base-26 numeric logic to compute the final result.
Time Complexity: O(n)
Space Complexity: O(1)
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.
def titleToNumberRec(columnTitle: str, index: int) -> int:
if index == len(columnTitle):
return 0
current = ord(columnTitle[index]) - ord('A') + 1
return current + 26 * titleToNumberRec(columnTitle, index + 1)
def titleToNumber(columnTitle: str) -> int:
return titleToNumberRec(columnTitle, 0)
# Example usage
title = "ZY"
print(titleToNumber(title))class Solution {
public int titleToNumberRec(String columnTitle, int index) {
if (index == columnTitle.length())
return 0;
int current = columnTitle.charAt(index) - 'A' + 1;
return current + 26 * titleToNumberRec(columnTitle, index + 1);
}
public int titleToNumber(String columnTitle) {
return titleToNumberRec(columnTitle, 0);
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.titleToNumber("ZY"));
}
}#include <iostream>
int titleToNumberRec(const std::string &columnTitle, int index) {
if (index == columnTitle.size())
return 0;
int current = columnTitle[index] - 'A' + 1;
return current + 26 * titleToNumberRec(columnTitle, index + 1);
}
int titleToNumber(const std::string &columnTitle) {
return titleToNumberRec(columnTitle, 0);
}
int main() {
std::string title = "ZY";
std::cout << titleToNumber(title) << std::endl;
return 0;
}function titleToNumberRec(columnTitle, index) {
if (index === columnTitle.length) return 0;
let current = columnTitle.charCodeAt(index) - 'A'.charCodeAt(0) + 1;
return current + 26 * titleToNumberRec(columnTitle, index + 1);
}
function titleToNumber(columnTitle) {
return titleToNumberRec(columnTitle, 0);
}
// Example Usage
console.log(titleToNumber("ZY"));#include <stdio.h>
int titleToNumberRec(char *columnTitle, int index) {
if (columnTitle[index] == '\0')
return 0;
int current = columnTitle[index] - 'A' + 1;
return current + 26 * titleToNumberRec(columnTitle, index + 1);
}
int titleToNumber(char *columnTitle) {
return titleToNumberRec(columnTitle, 0);
}
int main() {
char columnTitle[] = "AB";
printf("%d\n", titleToNumber(columnTitle));
return 0;
}using System;
class Solution {
public int TitleToNumberRec(string columnTitle, int index) {
if (index == columnTitle.Length)
return 0;
int current = columnTitle[index] - 'A' + 1;
return current + 26 * TitleToNumberRec(columnTitle, index + 1);
}
public int TitleToNumber(string columnTitle) {
return TitleToNumberRec(columnTitle, 0);
}
static void Main() {
Solution sol = new Solution();
Console.WriteLine(sol.TitleToNumber("ZY"));
}
}In the Python recursive solution, we define a helper function that recursively computes the column number by processing one character at a time, using recursion to handle the rest.
Time Complexity: O(n)
Space Complexity: O(n)
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).
function titleToNumber(columnTitle: string): number {
let ans: number = 0;
for (const c of columnTitle) {
ans = ans * 26 + (c.charCodeAt(0) - 'A'.charCodeAt(0) + 1);
}
return ans;
}func titleToNumber(columnTitle string) (ans int) {
for _, c := range columnTitle {
ans = ans*26 + int(c-'A'+1)
}
return
}Complexity comparison
| Approach | Complexity |
|---|---|
| Iterative Approach Using Base Conversion | Time Complexity: O(n) |
| Recursive Approach | Time Complexity: O(n) |
| Base Conversion | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Base Conversion | O(n) | O(1) | Best general solution; minimal memory and simplest implementation |
| Recursive Approach | O(n) | O(n) | Useful for explaining positional base‑26 logic recursively |
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.
