Difficulty: Easy | Acceptance: 67.70% | Paid: No Topics: Math, String
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 …
- Examples
- Constraints
- Approach 1: Iterative Left-to-Right
- Approach 2: Iterative Right-to-Left
- Approach 3: Recursive
Examples
Input: columnTitle = "A"
Output: 1
Input: columnTitle = "AB"
Output: 28
Input: columnTitle = "ZY"
Output: 701
Constraints
1 <= columnTitle.length <= 7
columnTitle consists only of uppercase English letters.
columnTitle is in the range ["A", "FXSHRXW"].
Approach 1: Iterative Left-to-Right
Intuition This problem is essentially a base conversion task, similar to converting a binary string to a decimal number, but here the base is 26. We can process the string from left to right, accumulating the result. For each new character, we multiply the current result by 26 (shifting the current digits to the left) and add the value of the new character.
Steps
- Initialize a variable
resultto 0. - Iterate through each character in the string
columnTitle. - For each character, calculate its value:
val = char - 'A' + 1. - Update the result:
result = result * 26 + val. - Return the final
result.
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for char in columnTitle:
value = ord(char) - ord('A') + 1
result = result * 26 + value
return resultComplexity
- Time: O(n), where n is the length of the string. We iterate through the string once.
- Space: O(1), we only use a constant amount of extra space for variables.
- Notes: This is the most efficient and standard approach for this problem.
Approach 2: Iterative Right-to-Left
Intuition We can also view this as a polynomial sum where the rightmost character is multiplied by 26⁰, the next by 26¹, and so on. We iterate from the end of the string to the beginning, calculating the contribution of each character based on its position.
Steps
- Initialize
resultto 0 andpowerto 1. - Iterate through the string from the last character to the first.
- Calculate the value of the current character.
- Add
value * powertoresult. - Multiply
powerby 26 for the next iteration. - Return
result.
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
power = 1
for char in reversed(columnTitle):
value = ord(char) - ord('A') + 1
result += value * power
power *= 26
return resultComplexity
- Time: O(n), where n is the length of the string.
- Space: O(1), constant space usage.
- Notes: Slightly less intuitive than the left-to-right approach but mathematically equivalent.
Approach 3: Recursive
Intuition
The problem can be defined recursively. The value of a string is 26 times the value of the string excluding the last character, plus the value of the last character. f(s) = 26 * f(s[0:n-1]) + val(s[n-1]).
Steps
- Base case: If the string is empty, return 0.
- Recursive step: Calculate the value of the last character.
- Return
26 * recursive_call(prefix) + last_char_value.
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
if not columnTitle:
return 0
last_val = ord(columnTitle[-1]) - ord('A') + 1
return 26 * self.titleToNumber(columnTitle[:-1]) + last_valComplexity
- Time: O(n), where n is the length of the string.
- Space: O(n), due to the recursion stack depth.
- Notes: While elegant, this approach uses more stack memory than the iterative solutions.