168. Excel Sheet Column Title LeetCode Solution
In this guide, you will get 168. Excel Sheet Column Title LeetCode Solution with the best time and space complexity. The solution to Excel Sheet Column Title 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 Title solution in C++
- Excel Sheet Column Title solution in Java
- Excel Sheet Column Title solution in Python
- Additional Resources
Problem Statement of Excel Sheet Column Title
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 – 1
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
168. Excel Sheet Column Title LeetCode Solution in C++
class Solution {
public:
string convertToTitle(int n) {
return n == 0 ? ""
: convertToTitle((n - 1) / 26) + (char)('A' + ((n - 1) % 26));
}
};
/* code provided by PROGIEZ */
168. Excel Sheet Column Title LeetCode Solution in Java
class Solution {
public String convertToTitle(int n) {
return n == 0 ? "" : convertToTitle((n - 1) / 26) + (char) ('A' + ((n - 1) % 26));
}
}
// code provided by PROGIEZ
168. Excel Sheet Column Title LeetCode Solution in Python
class Solution:
def convertToTitle(self, n: int) -> str:
return (self.convertToTitle((n - 1) // 26) + chr(ord('A') + (n - 1) % 26)
if n
else '')
# 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.