Skip to main content

Excel Sheet Column Title - Solution & Explanation

EasyMathString11 min readAsked at: Amazon, Microsoft, Meta +7
Practice this problem

Problem Statement

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

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

 

Example 1:

Input: columnNumber = 1
Output: "A"

Example 2:

Input: columnNumber = 28
Output: "AB"

Example 3:

Input: columnNumber = 701
Output: "ZY"

 

Constraints:

  • 1 <= columnNumber <= 231 - 1

Approach Overview

Problem Overview: Given a positive integer representing a column number in Excel, convert it to the corresponding column title. Excel uses a 1-indexed alphabet system: 1 → A, 2 → B, ..., 26 → Z, 27 → AA, 28 → AB.

Approach 1: Base 26 Conversion (O(log n) time, O(1) space)

This problem behaves like a base-26 number system but with a twist: digits range from 1–26 instead of 0–25. The fix is to decrement the number before each modulo operation. Repeatedly compute (n - 1) % 26 to determine the current character, map it to 'A' + remainder, append it to the result, and divide the number by 26 using integer division. Continue until the number becomes zero, then reverse the constructed string. The algorithm processes one character per iteration, so the runtime is O(log26 n), and only constant auxiliary space is used aside from the output string. This approach relies on simple arithmetic from Math and basic manipulation of a String.

Approach 2: Recursive Solution (O(log n) time, O(log n) space)

The same base‑26 idea can be expressed recursively. If the column number is zero, return an empty string. Otherwise, reduce the problem size by converting (n - 1) / 26 recursively, then append the current character derived from (n - 1) % 26. Each recursive call processes one “digit” in the Excel title representation. The recursion depth equals the number of characters in the final title, which is O(log26 n). Time complexity remains O(log n), but recursion introduces call stack overhead, giving O(log n) space complexity. This version is useful when practicing Recursion patterns or when building the string from left to right naturally.

Recommended for interviews: The iterative base‑26 conversion is the expected solution. It shows you understand the off‑by‑one detail caused by Excel’s 1-indexed alphabet mapping. Interviewers often look for the n-- adjustment before modulo. The recursive version is clean and expressive, but the iterative approach is simpler, avoids stack usage, and demonstrates stronger control over numeric conversions.

Approach 1: Base 26 Conversion

To solve this problem, we use a mathematical approach similar to converting a number to a different base. Here, the base is 26, similar to the English alphabet, where each letter corresponds to a unique representation of a number. This approach involves repeated division by 26 and mapping the remainder to the corresponding letter in the alphabet.

The function convertToTitle converts an integer to a column title by repeatedly dividing the number by 26 and using the remainder, adjusted by one, to map to a character. The result is constructed in reverse order, so it needs to be reversed before returning.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log26(n)), where n is the columnNumber, due to repeated division by 26.
Space Complexity: O(log26(n)), to store the result string.

Try this approach in the editor →

Approach 2: Recursive Solution

This approach uses recursion to solve the problem. The function calls itself with the quotient of the column number divided by 26, constructing the result string backwards using the modulo operation to index into the alphabet.

The recursive C function processes the column number by passing it with each reduced state until the base case is reached, building the result backwards by appending characters to the result as the recursive stack unwinds.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log26(n))
Space Complexity: O(log26(n)), due to the recursive call stack.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

Go

TypeScript

Rust

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Base 26 Conversion

Time Complexity: O(log26(n)), where n is the columnNumber, due to repeated division by 26.
Space Complexity: O(log26(n)), to store the result string.

Recursive Solution

Time Complexity: O(log26(n))
Space Complexity: O(log26(n)), due to the recursive call stack.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Base 26 Conversion (Iterative)O(log n)O(1)Best general solution. Efficient and commonly expected in interviews.
Recursive ConversionO(log n)O(log n)Useful for practicing recursion or when expressing base conversions recursively.

Video Solution

Excel Sheet Column Title | LeetCode 168 | C++, PythonKnowledge Center34,468 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Excel Sheet Column Title easy or hard?
Excel Sheet Column Title is classified as an Easy problem. The main challenge is recognizing the 1‑indexed base‑26 system used by Excel columns. Once the (n - 1) adjustment is understood, the implementation becomes straightforward.
Excel Sheet Column Title Python/Java solution
Python, Java, C++, C#, and JavaScript implementations all follow the same logic: repeatedly compute (n - 1) % 26 to get the current letter and divide the number by 26. The difference is only in syntax for string building and character conversion.
How to solve Excel Sheet Column Title in O(log n)?
Use a modified base‑26 conversion. Decrement the column number before applying modulo: remainder = (n - 1) % 26. Convert the remainder to a character using 'A' + remainder, append it to the result, then update n = (n - 1) / 26. Repeat until n becomes zero and reverse the string at the end.
What is the best approach for Excel Sheet Column Title?
The base‑26 conversion approach is the most efficient and widely expected solution. Repeatedly compute (n - 1) % 26 to determine the current character and divide the number by 26 to move to the next position. This handles Excel’s 1‑indexed alphabet mapping correctly. The algorithm runs in O(log n) time and O(1) extra space.
Is Excel Sheet Column Title asked at Google/Amazon/Meta?
This problem appears frequently in coding interviews at companies that emphasize basic algorithmic reasoning, including Amazon and Microsoft. It tests understanding of number systems, edge cases in indexing, and careful handling of modulo arithmetic.
What data structure is used in Excel Sheet Column Title?
The solution mainly uses arithmetic operations and string construction. No complex data structure is required. A simple string builder or character array is enough to accumulate characters during the base‑26 conversion process.
What is the time complexity of Excel Sheet Column Title?
The time complexity is O(log26 n), commonly written as O(log n). Each iteration extracts one character of the resulting Excel column title, reducing the number by a factor of 26. Since the number of characters in the output grows logarithmically with the input value, the algorithm scales efficiently.

Ready to solve this problem?

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

Practice on FleetCode