Please upgrade to NeetCode Pro to view company tags.
Prerequisites
Before attempting this problem, you should be comfortable with:
Number Base Conversion - Converting numbers between different bases (especially base-26)
Modular Arithmetic - Using modulo and integer division to extract digits
Recursion - Breaking down problems into smaller subproblems with base cases
ASCII/Character Manipulation - Converting between characters and their numeric values
1. Recursion
Intuition
Excel columns use a bijective base-26 system where A=1, B=2, ..., Z=26. Unlike standard base conversion where digits range from 0 to base-1, here digits range from 1 to 26. This means we need to adjust by subtracting 1 before finding each digit.
After subtracting 1, we can use modulo 26 to find the rightmost character and divide by 26 to get the remaining prefix. Recursion handles this naturally: first solve for the prefix (if any), then append the current character.
Algorithm
Base case: if columnNumber is 0, return an empty string.
Subtract 1 from columnNumber to convert to 0-indexed.
Recursively call for n // 26 to get the prefix string.
Compute the current character as chr('A' + n % 26).
Return the prefix concatenated with the current character.
publicclassSolution{publicStringconvertToTitle(int columnNumber){if(columnNumber ==0){return"";}int n = columnNumber -1;returnconvertToTitle(n /26)+(char)('A'+ n %26);}}
classSolution{public:
string convertToTitle(int columnNumber){if(columnNumber ==0){return"";}int n = columnNumber -1;returnconvertToTitle(n /26)+char('A'+ n %26);}};
funcconvertToTitle(columnNumber int)string{if columnNumber ==0{return""}
n := columnNumber -1returnconvertToTitle(n/26)+string(rune('A'+n%26))}
class Solution {funconvertToTitle(columnNumber: Int): String {if(columnNumber ==0){return""}val n = columnNumber -1returnconvertToTitle(n /26)+('A'+ n %26).toChar()}}
classSolution{funcconvertToTitle(_ columnNumber:Int)->String{if columnNumber ==0{return""}let n = columnNumber -1returnconvertToTitle(n /26)+String(Character(UnicodeScalar(Int(Character("A").asciiValue!)+ n %26)!))}}
Time & Space Complexity
Time complexity: O(logn)
Space complexity: O(logn) for recursion stack.
Where n is the given column number.
2. Iteration
Intuition
The iterative approach works from right to left, building the result string in reverse. At each step, we extract the rightmost character, then reduce the number for the next iteration. Since we build characters from least significant to most significant, we reverse the result at the end.
This avoids recursion overhead and makes the process explicit: subtract 1, find the character via modulo, divide by 26, and repeat until the number becomes 0.
Algorithm
Initialize an empty list res to collect characters.
funcconvertToTitle(columnNumber int)string{
res :=[]byte{}for columnNumber >0{
columnNumber--
offset := columnNumber %26
res =append(res,byte('A'+offset))
columnNumber /=26}for i, j :=0,len(res)-1; i < j; i, j = i+1, j-1{
res[i], res[j]= res[j], res[i]}returnstring(res)}
class Solution {funconvertToTitle(columnNumber: Int): String {var num = columnNumber
val res =StringBuilder()while(num >0){
num--val offset = num %26
res.append(('A'+ offset).toChar())
num /=26}return res.reverse().toString()}}
classSolution{funcconvertToTitle(_ columnNumber:Int)->String{var num = columnNumber
var res =[Character]()while num >0{
num -=1let offset = num %26
res.append(Character(UnicodeScalar(Int(Character("A").asciiValue!)+ offset)!))
num /=26}returnString(res.reversed())}}
Time & Space Complexity
Time complexity: O(logn)
Space complexity: O(1) extra space.
Where n is the given column number.
Common Pitfalls
Treating It as Standard Base-26 Conversion
Unlike standard base conversion where digits range from 0 to 25, Excel columns use a 1-indexed system where A=1 and Z=26. Forgetting to subtract 1 before taking modulo will cause off-by-one errors. For example, column 26 should be "Z", but without the subtraction, you would incorrectly compute the character for position 0.
Forgetting to Reverse the Result in Iterative Approach
When building the string iteratively by repeatedly taking modulo and dividing, you generate characters from least significant (rightmost) to most significant (leftmost). If you forget to reverse the accumulated characters at the end, "ABC" would incorrectly become "CBA". Either build the string in reverse or explicitly reverse it before returning.