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 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to translate a number into its corresponding Excel column letters, but instead of using a clever mathematical formula, you decide to recreate the exact sequence of how columns are named in order. The brute force approach builds and checks every single letter combination from the very beginning until it matches your target number.
Here's how the algorithm would work step-by-step:
def convertToTitle(columnNumber):
import itertools
current_count = 0
# Systematically check every possible length from 1 upwards to match the target position
for length in range(1, 10):
for candidate_tuple in itertools.product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=length):
current_count += 1
# Stop once the running count matches the target number to ensure correct mapping
if current_count == columnNumber:
return ''.join(candidate_tuple)
return ""The goal is to translate a standard counting number into its corresponding Excel column letters, which work just like a regular alphabet-based numbering system. Since Excel columns shift letters instead of using a standard zero-based placeholder, the secret is to continuously peel off the rightmost letter by shifting our perspective of the number step by step.
Here's how the algorithm would work step-by-step:
def convert_to_title(column_number): collected_letters = [] while column_number > 0: # Decrementing by one adjusts for 1-based indexing so modulo aligns with alphabet indices correctly. column_number -= 1 remainder_index = column_number % 26 collected_letters.append(chr(remainder_index + ord('A'))) # Dividing by 26 shifts the number to process the next higher-order character position. column_number //= 26 # Reversing is required because characters were extracted sequentially from right to left. return "".join(collected_letters[::-1])| Case | How to Handle |
|---|---|
| Minimum positive input of 1 | The modulo operation correctly maps 1 to A without requiring any special base-offset adjustments. |
| Exact multiples of 26 such as 26 or 52 | Subtracting 1 before the modulo operation prevents Z from being incorrectly represented as a zero-indexed value. |
| Column numbers right at the transition boundaries like 26 to 27 or 702 to 703 | The decrement step ensures single-digit titles shift cleanly to double-digit titles without off-by-one errors. |
| Maximum standard integer limits for 32-bit signed integers | Using standard 64-bit integers or built-in language types prevents integer overflow during division and scaling. |
| Inputs with large values requiring deep multi-character titles like ZZZ | The iterative division loop scales logarithmically and handles arbitrarily large numbers efficiently. |
| Zero or negative inputs | The problem statement guarantees a positive integer, so these inputs are out of scope. |