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:
We need to convert a number into its corresponding Excel column title. The brute force way is like trying to build the title character by character, exploring all possible letters for each position until we get the correct title for the given number.
Here's how the algorithm would work step-by-step:
def excel_sheet_column_title_brute_force(column_number):
# Handle base cases first
if column_number <= 0:
return ""
title = ""
length = 0
while True:
length += 1
start = 0
# Calculate the starting number for the current length title
for i in range(length - 1):
start += 26 ** (i + 1)
# Iterate through all possible combinations of letters for current length.
for i in range(26 ** length):
current_number = start + i + 1
# If we found a match, return the title
if current_number == column_number:
temp_number = i
temp_title = ""
for _ in range(length):
temp_title = chr(65 + (temp_number % 26)) + temp_title
temp_number //= 26
return temp_title
We want to convert a number into its corresponding Excel column title (e.g., 1 -> A, 2 -> B, 27 -> AA). The key idea is to treat this as a base-26 conversion problem, similar to converting from decimal to binary, but with a slight twist due to the lack of a 'zero' digit.
Here's how the algorithm would work step-by-step:
def convert_to_title(column_number):
column_title = ""
while column_number > 0:
# Adjust number if divisible by 26
if column_number % 26 == 0:
column_number -= 1
# Get remainder
remainder = column_number % 26
# Convert remainder to character
column_title = chr(65 + remainder) + column_title
# Integer division to reduce the number
column_number //= 26
return column_title
| Case | How to Handle |
|---|---|
| Input is 0 | The problem specifies a positive integer input, so input 0 should either be considered invalid and throw an exception, or return an empty string if considered a valid but non-existent column. |
| Input is 1 | The smallest valid input should correctly return 'A'. |
| Input is 26 | The input representing the last single-letter column should correctly return 'Z'. |
| Input is 27 | Input where the output requires two letters should return 'AA'. |
| Large input (e.g., 2147483647) | The algorithm should handle arbitrarily large positive integers (up to the limits of the system's integer representation) without integer overflow or excessive memory usage, likely by performing division and modulo operations repeatedly. |
| Inputs close to multiples of 26 (e.g., 52, 53) | Correctly handle cases where the last digit needs to 'borrow' from the previous digits during conversion, returning 'AZ' for 52, 'BA' for 53. |
| Negative input | Negative input is invalid based on the problem description and should throw an IllegalArgumentException or return an empty string. |
| Input close to integer maximum value | Ensure the calculations (specifically division and modulo operations related to base 26) do not overflow when dealing with values close to the maximum integer value supported by the language. |