# Solution For Excel Sheet Column Title

Problem Statement:

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

columnNumber = 1 -> Returns “A”
columnNumber = 28 -> Returns “AB”

Example 1:

Input: columnNumber = 1
Output: “A”

Example 2:

Input: columnNumber = 701
Output: “ZY”

Approach:

In this problem, we have to convert an integer column number into an Excel sheet column title. Suppose we want to convert a column number ‘n’ to its corresponding column title, the algorithm follows these steps:

• Divide the given column number by 26 and add the remainder character to the title string.
• If the remainder is zero, then the last character should be “Z” instead of “0”.
• Reduce the column number by dividing it by 26.
• Repeat steps 1 to 3 until the column number becomes zero.

For example, suppose we want to convert the column number 28 to its corresponding column title:

• 28/26 = 1 remainder 2, add the second character ‘B’ to the title string.
• Reduce the column number by 2/26 = 0. We get column number = 1.
• Since remainder is not zero, add corresponding character ‘A’ to the title string. Title string becomes “AB”.

Now let’s look at the solution:

Java Code:

class Solution {
public String convertToTitle(int columnNumber) {
StringBuilder title = new StringBuilder();
while(columnNumber>0){
int rem = (columnNumber-1)%26; //Turning 1 to 26 to 0 to 25
title.append((char)(‘A’+rem));
columnNumber = (columnNumber-1)/26; //Turning back 0 to 25 to 1 to 26
}
return title.reverse().toString();
}
}

Python Code:

class Solution:
def convertToTitle(self, columnNumber: int) -> str:
title = “”
while columnNumber:
rem = (columnNumber-1)%26 #Turning 1 to 26 to 0 to 25
title += chr(ord(‘A’)+rem)
columnNumber = (columnNumber-1)//26 #Turning back 0 to 25 to 1 to 26
return title[::-1]

We have used StringBuilder in Java and a simple string in Python to store the title. We run a while loop until the columnNumber becomes zero. In each iteration, we calculate the remainder and add the corresponding character to the title. We also reduce the column number in each iteration. Finally, we return the reversed title string.

The time complexity of this algorithm is O(logn) as we keep dividing the column number by 26. The space complexity is also O(logn) as we store the title string, which can have a maximum length of logn.

## Step by Step Implementation For Excel Sheet Column Title

```public class Solution {
public String convertToTitle(int n) {
StringBuilder sb = new StringBuilder();
while (n > 0) {
n--;
sb.append((char)('A' + n % 26));
n /= 26;
}
return sb.reverse().toString();
}
}

/*

This solution uses the fact that in the Excel column naming system, the letters are assigned in increasing order from left to right.

Thus, we can keep track of the current column number, n, and convert it to a column title by repeatedly finding the remainder when n is divided by 26 and prepending the corresponding letter to the result.

Since we need to repeatedly divide n by 26, we can do this more efficiently by first subtracting 1 from n before entering the loop.

Then, when we encounter a column number that is a multiple of 26, we know that this corresponds to "Z" in the Excel column naming system, so we can simply append "Z" to the result and decrement n by 26.

*/```
```def column_title(n):
res = []
while n:
n, rem = divmod(n-1, 26)
res.append(chr(rem + ord('A')))
return ''.join(res[::-1])```
```var convertToTitle = function(n) {
let str = "";
while (n > 0) {
let rem = n % 26;
if (rem === 0) {
str = "Z" + str;
n = (n / 26) - 1;
} else {
str = String.fromCharCode((rem - 1) + 'A'.charCodeAt(0)) + str;
n = Math.floor(n / 26);
}
}
return str;
};```
```class Solution {
public:
string convertToTitle(int n) {
string res;
while (n) {
int rem = n % 26;
n /= 26;
if (rem == 0) {
res += 'Z';
n--;
}
else {
res += (rem - 1) + 'A';
}
}
reverse(res.begin(), res.end());
return res;
}
};```
```public class Solution {
public string ConvertToTitle(int n) {
// create a StringBuilder object to store the result
StringBuilder sb = new StringBuilder();

// loop while n is greater than 0
while (n > 0) {
// calculate the remainder when n is divided by 26
int remainder = n % 26;

// if the remainder is 0, we need to use 'Z' as the character
// and decrement n by 26
if (remainder == 0) {
sb.Insert(0, 'Z');
n = n / 26 - 1;
}
// otherwise, we can use the remainder as the character
// and decrement n by 1
else {
sb.Insert(0, (char)(remainder - 1 + 'A'));
n = n / 26;
}
}

// return the result stored in the StringBuilder
return sb.ToString();
}
}```

Scroll to Top

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]