Given a positive integer n, return its corresponding Excel column title. In Excel, column titles follow this pattern:
A, B, C, ..., Z, AA, AB, ..., AZ, BA, ..., ZZ, AAA, AAB, ...
This is similar to a base-26 numbering system, but instead of digits 0–25, the letters A–Z represent values 1–26.
Examples:
Input: n = 28
Output: AB
Explanation: 1 to 26 are A to Z, then 27 is AA and 28 is AB.Input: n = 13
Output: M
Explanation: M is the 13th character of alphabet.Input: n = 5473578
Output: KYJZF
Table of Content
Recursive Base-26 Conversion – O(log₂₆ n) Time and O(log₂₆ n) Space
The idea is to use recursion to convert the given column number into its corresponding alphabetic name using base 26.
- Divide the number by 26 and find the corresponding letter using the remainder, handling the remainder 0 as 'Z'.
- Recursively process the remaining number or quotient.
- Combine the letters in the correct order to get the column name.
Working of Approach:
- Establish a base case that returns an empty string when the number becomes zero.
- Recursively divide the number by 26 to break it down to its smallest components.
- Decrement the number before calculating the remainder to handle the 1-indexing.
- Convert the remainder to its corresponding alphabetic character by adding it to 'A'.
- Concatenate the recursive call result with the generated character to build the column name.
#include <bits/stdc++.h>
using namespace std;
string colName(int n)
{
// base case
if (n == 0)
return "";
// decrement n to handle 1 based indexing
n--;
return colName(n / 26) + (char)(n % 26 + 'A');
}
int main()
{
int n = 702;
cout << colName(n);
return 0;
}
class GFG {
static String colName(int n)
{
// base case
if (n == 0)
return "";
// decrement n to handle 1 based indexing
n--;
return colName(n / 26) + (char)(n % 26 + 'A');
}
public static void main(String[] args)
{
int n = 702;
System.out.println(colName(n));
}
}
def colName(n):
# base case
if n == 0:
return ""
# decrement n to handle 1 based indexing
n -= 1
return colName(n // 26) + chr(n % 26 + ord('A'))
if __name__ == "__main__":
n = 702
print(colName(n))
using System;
class GFG {
static string colName(int n)
{
// base case
if (n == 0)
return "";
// decrement n to handle 1 based indexing
n--;
return colName(n / 26) + (char)(n % 26 + 'A');
}
static void Main()
{
int n = 702;
Console.WriteLine(colName(n));
}
}
function colName(n)
{
// base case
if (n === 0)
return "";
// decrement n to handle 1 based indexing
n--;
return colName(Math.floor(n / 26))
+ String.fromCharCode(n % 26
+ "A".charCodeAt(0));
}
// Driver Code
let n = 702;
console.log(colName(n));
Output
ZZ
Iterative Base-26 Conversion – O(log₂₆ n) Time and O(log₂₆ n) Space
Idea: The idea is to convert the given number into a column name using a base-26 system, where letters from A to Z represent values from 1 to 26.
- Divide the number by 26 and use the remainder to find the corresponding letter.
- If the remainder is 0, use 'Z' and adjust the number accordingly.
- Repeat until the number becomes 0, then reverse the result to get the correct column name.
Note that subtraction of 1 when encountering a remainder of 0 (which maps to 'Z') is crucial because Excel's column naming system is essentially a 1-indexed, base-26 numbering scheme. By subtracting 1 during the division process, the algorithm ensures that these multiples correctly map to 'Z', 'AZ', 'BZ', and so on.
Working of Approach:
- Find the remainder when dividing the column number by 26 to determine the current letter.
- If the remainder is non-zero, convert it to the corresponding letter by subtracting 1 and adding 'A'.
- If the remainder is 0, append 'Z' and adjust the number by subtracting 1 from the integer division result.
- Continuously divide the number by 26 to move to the next digit.
- Reverse the generated string to get the correct column name.
Let us understand with an example, n = 3588
- n = 3588, n % 26 = 0: 0 corresponds to 'Z'. The resultant string will be "Z". Divide n by 26 and subtract 1 from it, so n = 137.
- n = 137, n % 26 = 7: 7 corresponds to 'G'. The resultant string will be "ZG". Divide n by 26, so n = 5.
- n = 5, n % 26 = 5: 5 corresponds to 'E'. The resultant string will be "ZGE". Divide n by 26, so n = 0.
- n = 0, stop the process.
- As the string is generated from right to left, reverse it to get the correct column name. The final string will be "EGZ".
#include <bits/stdc++.h>
using namespace std;
string colName(int n)
{
string res = "";
while (n > 0)
{
// Find remainder
int rem = n % 26;
// If remainder is 0, then a 'Z'
// must be there in output
if (rem == 0)
{
res += 'Z';
n = (n / 26) - 1;
}
// If remainder is non-zero
else
{
res += (rem - 1) + 'A';
n = n / 26;
}
}
// Reverse the string
reverse(res.begin(), res.end());
return res;
}
int main()
{
int n = 702;
cout << colName(n);
return 0;
}
import java.util.*;
class GFG {
static String colName(int n)
{
StringBuilder res = new StringBuilder();
while (n > 0) {
// Find remainder
int rem = n % 26;
// If remainder is 0, then a 'Z'
// must be there in output
if (rem == 0) {
res.append('Z');
n = (n / 26) - 1;
}
// If remainder is non-zero
else {
res.append((char)((rem - 1) + 'A'));
n = n / 26;
}
}
// Reverse the string
res.reverse();
return res.toString();
}
public static void main(String[] args)
{
int n = 702;
System.out.println(colName(n));
}
}
def colName(n):
res = ""
while n > 0:
# Find remainder
rem = n % 26
# If remainder is 0, then a 'Z'
# must be there in output
if rem == 0:
res += 'Z'
n = (n // 26) - 1
# If remainder is non-zero
else:
res += chr((rem - 1) + ord('A'))
n //= 26
# Reverse the string
return res[::-1]
if __name__ == "__main__":
n = 702
print(colName(n))
using System;
class GFG {
static string colName(int n)
{
string res = "";
while (n > 0) {
// Find remainder
int rem = n % 26;
// If remainder is 0, then a 'Z'
// must be there in output
if (rem == 0) {
res += 'Z';
n = (n / 26) - 1;
}
// If remainder is non-zero
else {
res += (char)((rem - 1) + 'A');
n /= 26;
}
}
// Reverse the string
char[] charArray = res.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
static void Main()
{
int n = 702;
Console.WriteLine(colName(n));
}
}
function colName(n)
{
let res = "";
while (n > 0) {
// Find remainder
let rem = n % 26;
// If remainder is 0, then a 'Z'
// must be there in output
if (rem === 0) {
res += "Z";
n = Math.floor(n / 26) - 1;
}
// If remainder is non-zero
else {
res += String.fromCharCode((rem - 1)
+ "A".charCodeAt(0));
n = Math.floor(n / 26);
}
}
// Reverse the string
return res.split("").reverse().join("");
}
// Driver Code
let n = 702;
console.log(colName(n));
Output
ZZ
Related Article :Â Find the Excel column number from the column title