171. Excel Sheet Column Number LeetCode Solution
In this guide, you will get 171. Excel Sheet Column Number LeetCode Solution with the best time and space complexity. The solution to Excel Sheet Column Number problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Excel Sheet Column Number solution in C++
- Excel Sheet Column Number solution in Java
- Excel Sheet Column Number solution in Python
- Additional Resources
Problem Statement of Excel Sheet Column Number
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 <= 7
columnTitle consists only of uppercase English letters.
columnTitle is in the range ["A", "FXSHRXW"].
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
171. Excel Sheet Column Number LeetCode Solution in C++
class Solution {
public:
int titleToNumber(string columnTitle) {
return accumulate(
columnTitle.begin(), columnTitle.end(), 0,
[](int subtotal, char c) { return subtotal * 26 + (c - 'A' + 1); });
}
};
/* code provided by PROGIEZ */
171. Excel Sheet Column Number LeetCode Solution in Java
class Solution {
public int titleToNumber(String columnTitle) {
return columnTitle.chars().reduce(0, (subtotal, c) -> subtotal * 26 + c - '@');
}
}
// code provided by PROGIEZ
171. Excel Sheet Column Number LeetCode Solution in Python
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
return functools.reduce(lambda subtotal, c:
subtotal * 26 + ord(c) - ord('@'), columnTitle, 0)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.