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
This problem involves converting a positive integer representing a column number in an Excel sheet to its corresponding column title (e.g., 1 -> "A", 27 -> "AA"). The solution leverages the base-26 number system (where digits are A-Z instead of 0-9).
Algorithm:
Iterative Conversion: The core idea is to repeatedly divide the input columnNumber
by 26. The remainder of each division represents a digit in the base-26 system (0 maps to 'A', 1 to 'B', ..., 25 to 'Z').
Remainder Handling: Before converting the remainder to a character, we decrement columnNumber
by 1. This is because the remainders are 0-indexed, but Excel columns are 1-indexed.
Character Conversion: The remainder is converted to its corresponding character using ASCII values ('A' has ASCII value 65).
String Building: The resulting characters are appended to a string or array. Since we process the digits from least to most significant, the string is reversed before returning.
Time and Space Complexity:
Time Complexity: O(log₂₆(columnNumber)). The number of iterations is proportional to the number of digits in the base-26 representation of the input number. This is logarithmic because the number of digits increases slowly with the input size.
Space Complexity: O(log₂₆(columnNumber)). The space used to store the result string (or array) is proportional to the number of digits in the base-26 representation, making it logarithmic.
Code Explanation (Python):
class Solution:
def convertToTitle(self, columnNumber: int) -> str:
res = []
while columnNumber:
columnNumber -= 1 # Adjust for 1-based indexing
res.append(chr(ord('A') + columnNumber % 26)) #Append character to the list
columnNumber //= 26 # Integer division
return ''.join(res[::-1]) # Reverse the list and join it to create a string.
The Python code directly implements the algorithm. It iteratively calculates the remainders, converts them to characters, and builds the result string. The [::-1]
slicing efficiently reverses the list.
Code Explanation (Java):
class Solution {
public String convertToTitle(int columnNumber) {
StringBuilder res = new StringBuilder();
while (columnNumber != 0) {
columnNumber--; // Adjust for 1-based indexing
res.append((char) ('A' + columnNumber % 26)); //Append character to the StringBuilder
columnNumber /= 26; //Integer division
}
return res.reverse().toString(); //Reverse the StringBuilder and convert it to a String.
}
}
The Java code uses a StringBuilder
for efficient string manipulation. It's functionally equivalent to the Python code.
The other languages (Go, TypeScript, Rust, C#) follow similar logic, adapting the syntax and data structures accordingly. The core algorithm remains consistent across all implementations.