HomeLeetcode168. Excel Sheet Column Title - Leetcode Solutions

168. Excel Sheet Column Title – Leetcode Solutions

Description

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 
...

Examples:

Example 1:

Input: columnNumber = 1
Output: "A"

Example 2:

Input: columnNumber = 28
Output: "AB"

Example 3:

Input: columnNumber = 701
Output: "ZY"

Solution in Python

Python
class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        result = []  # Initialize an empty list to store the characters of the resulting column title.
        
        while columnNumber > 0:
            columnNumber -= 1  # Adjust columnNumber to be 0-indexed.
            remainder = columnNumber % 26  # Find the remainder when columnNumber is divided by 26.
            result.append(chr(remainder + ord('A')))  # Convert the remainder to a corresponding letter and append to result.
            columnNumber //= 26  # Update columnNumber for the next iteration.
        
        result.reverse()  # The result list is in reverse order, so reverse it to get the correct title.
        return ''.join(result)  # Join the list of characters into a string and return it.

Detailed Explanation:

  1. Initialization:
    • We initialize an empty list result to store the characters of the resulting column title.
  2. Loop until columnNumber is greater than 0:
    • Adjust columnNumber by subtracting 1. This adjustment is necessary because Excel columns are 1-indexed, but the 26 letters (A-Z) are 0-indexed in terms of remainder calculations.
  3. Finding the corresponding letter:
    • Compute the remainder when columnNumber is divided by 26. This remainder will determine the current letter.
    • Convert the remainder to the corresponding character by adding it to the ASCII value of ‘A’ (which is 65). Use chr() to get the character and append it to the result list.
  4. Update columnNumber:
    • Divide columnNumber by 26 using integer division to update it for the next iteration of the loop.
  5. Reverse and join:
    • After exiting the loop, the result list will contain the characters in reverse order (since we are appending the least significant character first). Therefore, reverse the list.
    • Join the characters in the list to form the final column title string and return it.

This approach effectively converts the given integer to its corresponding Excel column title by simulating the manual process of determining the letters, handling the 26-based system properly.

Solution in Javascript

JavaScript
/**
 * @param {number} columnNumber
 * @return {string}
 */
var convertToTitle = function(columnNumber) {
    let result = []; // Initialize an empty array to store the characters of the resulting column title.
    
    while (columnNumber > 0) {
        columnNumber--; // Adjust columnNumber to be 0-indexed.
        let remainder = columnNumber % 26; // Find the remainder when columnNumber is divided by 26.
        result.push(String.fromCharCode(remainder + 65)); // Convert the remainder to a corresponding letter and append to result.
        columnNumber = Math.floor(columnNumber / 26); // Update columnNumber for the next iteration.
    }
    
    result.reverse(); // The result array is in reverse order, so reverse it to get the correct title.
    return result.join(''); // Join the array of characters into a string and return it.
};

Solution in Java

Java
class Solution {
    public String convertToTitle(int columnNumber) {
        StringBuilder result = new StringBuilder(); // Initialize a StringBuilder to store the characters of the resulting column title.
        
        while (columnNumber > 0) {
            columnNumber--; // Adjust columnNumber to be 0-indexed.
            int remainder = columnNumber % 26; // Find the remainder when columnNumber is divided by 26.
            result.append((char)(remainder + 'A')); // Convert the remainder to a corresponding letter and append to result.
            columnNumber /= 26; // Update columnNumber for the next iteration.
        }
        
        result.reverse(); // The result is in reverse order, so reverse it to get the correct title.
        return result.toString(); // Convert the StringBuilder to a string and return it.
    }
}

Solution in C#

C#
public class Solution {
    public string ConvertToTitle(int columnNumber) {
        StringBuilder result = new StringBuilder(); // Initialize a StringBuilder to store the characters of the resulting column title.
        
        while (columnNumber > 0) {
            columnNumber--; // Adjust columnNumber to be 0-indexed.
            int remainder = columnNumber % 26; // Find the remainder when columnNumber is divided by 26.
            result.Append((char)(remainder + 'A')); // Convert the remainder to a corresponding letter and append to result.
            columnNumber /= 26; // Update columnNumber for the next iteration.
        }
        
        // The result is in reverse order, so reverse it to get the correct title.
        char[] charArray = result.ToString().ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray); // Convert the reversed char array to a string and return it.
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular