Skip to main content

Excel Sheet Column Title

LeetCode 168 | Difficulty: Easy​

Easy

Problem Description​

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 <= 2^31 - 1`

Topics: Math, String


Approach​

Mathematical​

Look for mathematical patterns or formulas. Consider: modular arithmetic, GCD/LCM, prime factorization, combinatorics, or geometric properties.

When to use

Problems with clear mathematical structure, counting, number properties.

String Processing​

Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.

When to use

Anagram detection, palindrome checking, string transformation, pattern matching.


Solutions​

Solution 1: C# (Best: 108 ms)​

MetricValue
Runtime108 ms
MemoryN/A
Date2018-03-30
Solution
public class Solution {
public string ConvertToTitle(int n) {
string sb = string.Empty;
while(n>0)
{
n--;
var rem = n%26;
n=n/26;
sb = ((char)('A'+rem))+ sb;

}
return sb;
}
}

Complexity Analysis​

ApproachTimeSpace
Solution$O(n)$$O(1) to O(n)$

Interview Tips​

Key Points
  • Start by clarifying edge cases: empty input, single element, all duplicates.