KatInterview

168. Excel Sheet Column Title

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to convert a given positive integer, representing a column number in an Excel sheet, into its corresponding alphabetical column title. The mapping is A=1, B=2, ..., Z=26, AA=27, AB=28, and so on. This is a base conversion problem, specifically from base-10 to a custom base-26 system.

Solution

Excel Sheet Column Title in Python

class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        # A list to store characters in reverse order
        result = []
        while columnNumber > 0:
            # Adjust to a 0-based index (0-25) from a 1-based index (1-26)
            columnNumber -= 1
            
            # Find the remainder to determine the character
            remainder = columnNumber % 26
            result.append(chr(ord('A') + remainder))
            
            # Integer division to get the number for the next digit
            columnNumber //= 26
        
        # The list contains characters in reverse order (e.g., for 28 -> ['B', 'A'])
        # Reverse the list and join to form the final string
        return "".join(result[::-1])

Excel Sheet Column Title in Java

class Solution {
    public String convertToTitle(int columnNumber) {
        StringBuilder result = new StringBuilder();
        while (columnNumber > 0) {
            // This maps the 1-based system (1-26) to a 0-based system (0-25)
            // for easier base-26 conversion.
            columnNumber--;
            
            // Get the remainder, which corresponds to the character.
            // 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z'
            int remainder = columnNumber % 26;
            result.append((char)('A' + remainder));
            
            // Update columnNumber to the value for the next "digit".
            columnNumber /= 26;
        }
        
        // The characters are appended, so they are in reverse order.
        // For example, 28 gives 'B' then 'A'. The StringBuilder becomes "BA". Reverse to get "AB".
        return result.reverse().toString();
    }
}

Excel Sheet Column Title in C++

class Solution {
public:
    string convertToTitle(int columnNumber) {
        string result = "";
        while (columnNumber > 0) {
            // This maps the 1-based system (1-26) to a 0-based system (0-25),
            // which makes the base-26 conversion straightforward.
            columnNumber--;
            
            // Get the remainder, which corresponds to the character.
            // 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z'
            int remainder = columnNumber % 26;
            result += (char)('A' + remainder);
            
            // Update columnNumber for the next "digit" in the base-26 system.
            columnNumber /= 26;
        }
        
        // The characters are added to the string, building it in reverse.
        // For example, 28 -> "B" then "A". result becomes "BA". We need to reverse it to "AB".
        reverse(result.begin(), result.end());
        
        return result;
    }
};

Excel Sheet Column Title in TypeScript

function convertToTitle(columnNumber: number): string {
    const result: string[] = [];
    while (columnNumber > 0) {
        // Adjust from 1-based (1-26) to 0-based (0-25) for calculation
        columnNumber--;

        // Get the remainder which will be in the range 0-25
        const remainder = columnNumber % 26;
        // Convert remainder to its corresponding character ('A'-'Z')
        result.push(String.fromCharCode('A'.charCodeAt(0) + remainder));

        // Update columnNumber for the next iteration (integer division)
        columnNumber = Math.floor(columnNumber / 26);
    }
    
    // The characters were added from right to left, so reverse the array and join.
    return result.reverse().join('');
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.