Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
Input: columnTitle = "A" Output: 1
Example 2:
Input: columnTitle = "AB" Output: 28
Example 3:
Input: columnTitle = "ZY" Output: 701
Constraints:
1 <= columnTitle.length <= 7columnTitle consists only of uppercase English letters.columnTitle is in the range ["A", "FXSHRXW"].When 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:
The problem asks to convert a column title like 'AB' to its corresponding column number. The brute force way to do this is similar to trying all possible number combinations, but with letters. We can think of each letter as a digit in a base-26 number system.
Here's how the algorithm would work step-by-step:
def excel_sheet_column_number_brute_force(column_title):
result = 0
for i in range(len(column_title)):
# Iterate through each character of the column title
character = column_title[i]
letter_value = ord(character) - ord('A') + 1
# For letters beyond the first, update the result
if i < len(column_title) - 1:
result = result + letter_value * (26 ** (len(column_title) - i - 1))
# Add the value of the right-most character
else:
result = result + letter_value
return resultThe problem is like figuring out what number a name like 'AB' represents in a spreadsheet. Each letter's position matters, with later letters being 'worth' more, similar to how digits in a number system work.
Here's how the algorithm would work step-by-step:
def excel_sheet_column_number(column_title):
running_total = 0
for character in column_title:
#Convert the character to its numerical value (A=1, B=2, ..., Z=26)
character_value = ord(character) - ord('A') + 1
# Multiply the current total by 26
running_total = running_total * 26
# Add the numerical value of the current letter to the total
running_total += character_value
return running_total| Case | How to Handle |
|---|---|
| Null or empty input string | Return 0 immediately as there is no column title. |
| Single-character input (e.g., "A") | The algorithm correctly handles single-character inputs by converting the character to its corresponding numerical value (A=1, B=2, etc.). |
| Input string with mixed-case characters (e.g., "aB") | Convert the input to uppercase to ensure uniformity and correct calculation. |
| Very long input string representing a large column number (e.g., "FXSHRXW") | Ensure that the intermediate calculations do not result in integer overflow; use a 64-bit integer type if necessary. |
| Input string contains invalid characters (e.g., "A!C") | Throw an exception or return an error value, as the problem statement defines the input string to only contain letters. |
| Input string "Z" | The algorithm should correctly convert "Z" to 26. |
| Input string "AA" | The algorithm should correctly convert "AA" to 27 (1 * 26 + 1). |
| Maximum possible input string (e.g., "XFD" which is 2^14 -1 ) | The algorithm should handle the largest column number and ensure no overflow; data type used to hold result must be large enough. |