Talentd
FleetCode
Make and follow sheets.Sign in

Excel Sheet Column Title solution and explanation

EasyMathString4 min read47.9% acceptanceUpdated 9 Apr 2026
Excel Sheet Column Title | LeetCode 168 | C++, PythonKnowledge Center · 14:37 · 34K viewsWatch on YouTube
9 more videos on this problem
  1. Excel Sheet Column Title | LeetCode 168 | C++, PythonKnowledge Center · 14:37 · 34K views
  2. Excel Sheet Column Title - Leetcode 168 - PythonNeetCodeIO · 11:50 · 25K views
  3. 168. Excel Sheet Column Title || Java || Leetcode || HindiCoding Sphere · 6:03 · 9K views
  4. Excel Sheet Column Title (Leetcode 168) Solution in Java | Excel Sheet Column Title | Leetcode 168Economic Coder · 10:11 · 4K views
  5. Excel Sheet Column Title, Leetcode 168Hansraj Das · 8:15 · 562 views
  6. 💻Solving All LeetCode Problems with PYTHON 🐍 - 168 Excel Sheet Column TitleAlex Brou · 9:57 · 469 views
  7. LeetCode 168 : Excel Sheet Column Title || EASY || JAVA || Detailed SolutionCodeGPT · 9:13 · 459 views
  8. Daily Leetcode #1239: Aug 22, 2023 - 168. Excel Sheet Column TitleProgramming Live with Larry · 7:02 · 350 views
  9. 168. Excel Sheet Column Title | Leetcode | PythonAbdullah Arshed · 3:42 · 301 views
  10. Leetcode 168 | | Excel Sheet Column Title | |C++Build With Omkar · 5:11 · 297 views

The problem

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.

def convert_to_title(column_number: int) -> str:
    result = []
    while column_number > 0:
        column_number -= 1
        result.append(chr((column_number % 26) + ord('A')))
        column_number //= 26
    return ''.join(reversed(result))

print(convert_to_title(28))  # Output: AB
public class ExcelConverter {
    public String convertToTitle(int columnNumber) {
        StringBuilder result = new StringBuilder();
        while (columnNumber > 0) {
            columnNumber--;
            result.insert(0, (char) ((columnNumber % 26) + 'A'));
            columnNumber /= 26;
        }
        return result.toString();
    }
    public static void main(String[] args) {
        ExcelConverter ec = new ExcelConverter();
        System.out.println(ec.convertToTitle(28));  // Output: AB
    }
}
#include <iostream>
#include <string>

std::string convertToTitle(int columnNumber) {
    std::string result = "";
    while (columnNumber > 0) {
        columnNumber--;
        result = char((columnNumber % 26) + 'A') + result;
        columnNumber /= 26;
    }
    return result;
}

int main() {
    std::cout << convertToTitle(28) << std::endl;  // Output: AB
    return 0;
}
function convertToTitle(columnNumber) {
    let result = '';
    while (columnNumber > 0) {
        columnNumber--;
        result = String.fromCharCode((columnNumber % 26) + 'A'.charCodeAt(0)) + result;
        columnNumber = Math.floor(columnNumber / 26);
    }
    return result;
}

console.log(convertToTitle(28));  // Output: AB
#include <stdio.h>
#include <stdlib.h>

char* convertToTitle(int columnNumber) {
    char *result = (char*) malloc(10 * sizeof(char));
    int index = 0;
    while (columnNumber > 0) {
        columnNumber--;
        result[index++] = (columnNumber % 26) + 'A';
        columnNumber /= 26;
    }
    result[index] = '\0';
    for (int i = 0; i < index / 2; i++) {
        char temp = result[i];
        result[i] = result[index - i - 1];
        result[index - i - 1] = temp;
    }
    return result;
}

int main() {
    printf("%s\n", convertToTitle(28));  // Output: AB
    return 0;
}
using System;
using System.Text;

public class ExcelConverter {
    public string ConvertToTitle(int columnNumber) {
        StringBuilder result = new StringBuilder();
        while (columnNumber > 0) {
            columnNumber--;
            result.Insert(0, (char)((columnNumber % 26) + 'A'));
            columnNumber /= 26;
        }
        return result.ToString();
    }

    public static void Main(){
        ExcelConverter ec = new ExcelConverter();
        Console.WriteLine(ec.ConvertToTitle(28));  // Output: AB
    }
}

In Python, we use a list to store characters and later join them into a string. We map the remainder of the division to corresponding characters using chr and ord.

Complexity

Time Complexity: O(log26(n))
Space Complexity: O(log26(n))

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.

def convert_to_title(column_number: int) -> str:
    if column_number <= 0:
        return ''
    column_number -= 1
    return convert_to_title(column_number // 26) + chr((column_number % 26) + ord('A'))

print(convert_to_title(28))  # Output: AB
public class ExcelConverter {
    public String convertToTitle(int columnNumber) {
        if (columnNumber <= 0) return "";
        columnNumber--;
        String result = convertToTitle(columnNumber / 26);
        return result + (char)((columnNumber % 26) + 'A');
    }

    public static void main(String[] args) {
        ExcelConverter ec = new ExcelConverter();
        System.out.println(ec.convertToTitle(28));  // Output: AB
    }
}
#include <iostream>
#include <string>

void convertToTitleRec(int columnNumber, std::string &result) {
    if (columnNumber <= 0) return;
    columnNumber--;
    convertToTitleRec(columnNumber / 26, result);
    result += (char)((columnNumber % 26) + 'A');
}

std::string convertToTitle(int columnNumber) {
    std::string result = "";
    convertToTitleRec(columnNumber, result);
    return result;
}

int main() {
    std::cout << convertToTitle(28) << std::endl;  // Output: AB
    return 0;
}
function convertToTitle(columnNumber) {
    if (columnNumber <= 0) return '';
    columnNumber--;
    return convertToTitle(Math.floor(columnNumber / 26)) + String.fromCharCode((columnNumber % 26) + 'A'.charCodeAt(0));
}

console.log(convertToTitle(28));  // Output: AB
#include <stdio.h>
#include <stdlib.h>

void convertToTitleRec(int columnNumber, char *result, int *index) {
    if (columnNumber <= 0) return;
    columnNumber--;
    convertToTitleRec(columnNumber / 26, result, index);
    result[(*index)++] = (columnNumber % 26) + 'A';
}

char* convertToTitle(int columnNumber) {
    char *result = (char*) malloc(10 * sizeof(char));
    int index = 0;
    convertToTitleRec(columnNumber, result, &index);
    result[index] = '\0';
    return result;
}

int main() {
    printf("%s\n", convertToTitle(28));  // Output: AB
    return 0;
}
using System;

public class ExcelConverter {
    public string ConvertToTitle(int columnNumber) {
        if (columnNumber <= 0) return string.Empty;
        columnNumber--;
        return ConvertToTitle(columnNumber / 26) + (char)((columnNumber % 26) + 'A');
    }

    public static void Main(){
        ExcelConverter ec = new ExcelConverter();
        Console.WriteLine(ec.ConvertToTitle(28));  // Output: AB
    }
}

The Python recursive solution builds the result by calling itself recursively, appending the correct character corresponding to each 'digit' of the base 26 representation.

Complexity

Time Complexity: O(log26(n))
Space Complexity: O(log26(n))

Try this approach in the editor

Approach 3: Default Approach

function convertToTitle(columnNumber: number): string {
    let res: string[] = [];
    while (columnNumber > 0) {
        --columnNumber;
        let num: number = columnNumber % 26;
        res.unshift(String.fromCharCode(num + 65));
        columnNumber = Math.floor(columnNumber / 26);
    }
    return res.join('');
}
func convertToTitle(columnNumber int) string {
	res := []rune{}
	for columnNumber != 0 {
		columnNumber -= 1
		res = append([]rune{rune(columnNumber%26 + int('A'))}, res...)
		columnNumber /= 26
	}
	return string(res)
}
impl Solution {
    #[allow(dead_code)]
    pub fn convert_to_title(column_number: i32) -> String {
        let mut ret = String::from("");
        let mut column_number = column_number;

        while column_number > 0 {
            if column_number <= 26 {
                ret.push((('A' as u8) + (column_number as u8) - 1) as char);
                break;
            } else {
                let mut left = column_number % 26;
                left = if left == 0 { 26 } else { left };
                ret.push((('A' as u8) + (left as u8) - 1) as char);
                column_number = (column_number - 1) / 26;
            }
        }

        ret.chars().rev().collect()
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Base 26 Conversion

Time Complexity: O(log26(n))
Space Complexity: O(log26(n))

Recursive Solution

Time Complexity: O(log26(n))
Space Complexity: O(log26(n))

Default Approach–

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.

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.

Comments

Now write it yourselfClose this page and solve Excel Sheet Column Title in the editor, without looking.
Practise Excel Sheet Column Title