Excel Sheet Column Title - Decoding Base-26 Conversion
LeetCode #168 (Easy)
Today, we're tackling "Excel Sheet Column Title" (LeetCode #168).
This problem asks us to convert a given integer columnNumber into its corresponding Excel column title (like A, B, Z, AA, AB, etc.). While it looks like a simple base conversion, there's a unique characteristic that requires a slight adjustment to the standard algorithm. Let's decode this base-26 puzzle!
Understanding the Problem:
We are given an integer columnNumber (e.g., 1, 2, 26, 27, 28). Our task is to return its corresponding column title as it appears in an Excel sheet.
The mapping is as follows:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
AZ -> 52
BA -> 53
...
Notice how it behaves like a base-26 system, but with a crucial difference: there's no '0' equivalent. 'Z' represents 26, not 0, and it acts as the "last digit" in a place value. This is often called a "1-indexed base-26" system.
Examples:
Input:
columnNumber = 1- Output:
"A"
- Output:
Input:
columnNumber = 28- Output:
"AB"(26 * 1 + 2)
- Output:
Input:
columnNumber = 52- Output:
"AZ"(26 * 1 + 26)
- Output:
Input:
columnNumber = 701- Output:
"ZY"(26 * 26 + 25)
- Output:
My Thought Process & Approach (Modified Base-26 Conversion):
At first glance, this problem screams "base conversion!" It's similar to converting a decimal number to binary (base-2) or hexadecimal (base-16) using repeated division and modulo operations.
The Standard Base Conversion Algorithm: To convert a decimal number N to base B:
The last digit is
N % B.The remaining number is
N / B.Repeat until
Nbecomes 0.The digits are read in reverse order of generation.
The "Excel Twist": The challenge here is the 1-based indexing (A=1, Z=26) and the absence of a '0' digit. In a standard base-26 system, if val % 26 were 0, it would correspond to the 0th digit. But in Excel's system, 26 maps to Z, not some 'A-1'.
Consider columnNumber = 26:
- Standard:
26 % 26 = 0(digit 0),26 / 26 = 1. Then1 % 26 = 1(digit 1),1 / 26 = 0. This would give "BA" (1,0) if A was 1 and B was 2. This is incorrect. We want "Z".
The Fix: When val % 26 results in 0, it actually means the current "digit" should be 'Z' (representing the 26th letter). When this happens, we must treat 26 as the "remainder," and crucially, we must "borrow" from the next higher place value by decrementing the val before dividing it by 26. This handles the 'Z' case correctly, where Z effectively carries over a full '26' to the next 'column'.
Here's the refined logic:
Initialize
val = columnNumber,alph = 26, and an emptyStringBuilder sb.Loop while
valis not0:Calculate
last = val % alph. This is our potential digit.If
last == 0: This is the special case for 'Z'.Append 'Z' to
sb.Decrement
valby 1 (val--). This is the "borrow" to correctly handle the carry-over from 'Z' in the next division.
Else (
last != 0): This is a standard digit.- Append the character
(char)('A' + (last - 1))tosb. (e.g., iflastis 1,A + (1-1)gives 'A').
- Append the character
Divide
valbyalph(val = val / alph) to move to the next higher place value.
After the loop, the
StringBuildersbcontains the characters in reverse order (e.g., for 28 -> B then A). Reversesbto get the final title.Convert
sbtoStringand return it.
This iterative process correctly converts the 1-indexed number to its Excel column title.
Java Code Implementation:
class Solution {
public String convertToTitle(int columnNumber) {
int val = columnNumber;
int alph = 26;
StringBuilder sb = new StringBuilder();
while(val!=0){
int last = val%alph;
if(last == 0) {sb.append('Z'); val--;}
else sb.append((char)('A' + (last-1)));
val = val/alph;
}
sb = sb.reverse();
return sb.toString();
}
}
Time and Space Complexity Analysis:
Time Complexity: O(log{26} N)
In each iteration of the
whileloop, thevalis divided by26. This means the number of iterations is proportional to the logarithm base 26 of the inputcolumnNumber (N).Operations inside the loop (modulo, division, append) are constant time.
The
StringBuilder.reverse()andtoString()operations take time proportional to the length of the resulting string, which is also O(log{26} N).Therefore, the total time complexity is O(log{26} N).
Space Complexity: O(log{26} N)
The
StringBuilderstores the characters of the resulting column title. The length of this title is proportional to O(log{26} N).No other data structures are used whose size depends on the input
N.Therefore, the space complexity is O(log{26} N).