LeetCode Problem Workspace

Excel Sheet Column Title

Convert a positive integer to its corresponding Excel column title using base-26 math and string manipulation efficiently.

category

2

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Math plus String

bolt

Answer-first summary

Convert a positive integer to its corresponding Excel column title using base-26 math and string manipulation efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus String

Try AiBox Copilotarrow_forward

To solve Excel Sheet Column Title, map the integer to a pseudo-base-26 representation adjusting for 1-based indexing. Repeatedly extract letters from the least significant position by decrementing the number and using modulo arithmetic. Concatenate the letters in reverse to form the correct Excel-style column string, handling edge cases like multiples of 26 properly for accurate mapping.

Problem Statement

Given a positive integer columnNumber, return its corresponding column title as it appears in an Excel sheet. The title uses uppercase English letters in sequence where 1 maps to 'A', 2 to 'B', up to 26 for 'Z', then 27 becomes 'AA', 28 becomes 'AB', and so on.

Implement a function that converts the integer columnNumber to its Excel column string, ensuring correct handling of the 1-based offset and multiple-character sequences. For example, columnNumber = 1 returns 'A', columnNumber = 28 returns 'AB', and columnNumber = 701 returns 'ZY'.

Examples

Example 1

Input: See original problem statement.

Output: See original problem statement.

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

Example 2

Input: columnNumber = 1

Output: "A"

Example details omitted.

Example 3

Input: columnNumber = 28

Output: "AB"

Example details omitted.

Constraints

  • 1 <= columnNumber <= 231 - 1

Solution Approach

Use iterative modulo conversion

Subtract 1 from columnNumber to handle 1-based indexing, then repeatedly take modulo 26 to get the current character. Map 0–25 to 'A'–'Z' and prepend each character to build the final string until the number reduces to zero.

Handle edge cases for multiples of 26

When columnNumber is divisible by 26, the naive modulo may produce zero, which corresponds to 'Z'. Ensure you adjust the number by decrementing before modulo to correctly generate letters like 'Z', 'AZ', 'ZZ', and so forth.

Optimize for time and space

Since each iteration reduces the number by factor of 26, the loop runs in O(log N) time. Use a single string or list to accumulate characters, avoiding extra space allocations for efficiency, maintaining O(1) space complexity.

Complexity Analysis

Metric Value
Time O(\log N)
Space O(1)

The algorithm runs in O(log N) time because each division by 26 reduces the number. Space is O(1) as only the output string accumulates characters, with no additional data structures proportional to input size.

What Interviewers Usually Probe

  • The interviewer may hint at off-by-one errors when handling 'Z'.
  • Expect questions on handling large column numbers efficiently without recursion.
  • They may ask you to explain how base-26 mapping differs from standard decimal conversion.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to subtract 1 before modulo leads to incorrect letters for multiples of 26.
  • Prepending letters incorrectly can reverse the title and yield wrong results.
  • Not handling large numbers properly can cause overflow if intermediate calculations assume 32-bit limits.

Follow-up variants

  • Return the column title in lowercase letters instead of uppercase.
  • Given a list of columnNumbers, return all corresponding Excel titles in one pass.
  • Implement the inverse function converting Excel titles back to integers.

FAQ

What is the pattern behind Excel Sheet Column Title conversion?

The pattern is a modified base-26 system where letters 'A'-'Z' represent digits 1-26, requiring decrement before modulo for correct mapping.

How do I handle columnNumber = 26 or multiples of 26?

Always subtract 1 before modulo; this adjustment ensures that 26 maps to 'Z', 52 maps to 'AZ', and similar sequences correctly.

Can this be solved recursively?

Yes, recursion is possible by dividing the number by 26 and prepending the current letter, but iterative is preferred for constant space efficiency.

What are the time and space complexities?

Time complexity is O(log N) because each step divides the number by 26. Space complexity is O(1), excluding the output string.

Does GhostInterview cover Math plus String patterns for this problem?

Yes, GhostInterview provides targeted explanations for combining modulo math with string manipulation specific to Excel Sheet Column Titles.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
5
6
7
8
class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        res = []
        while columnNumber:
            columnNumber -= 1
            res.append(chr(ord('A') + columnNumber % 26))
            columnNumber //= 26
        return ''.join(res[::-1])
Excel Sheet Column Title Solution: Math plus String | LeetCode #168 Easy