LeetCode 168: Excel Sheet Column Title Solution

Master LeetCode problem 168 (Excel Sheet Column Title), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

168. Excel Sheet Column Title

Problem Explanation

Explanation:

To convert a column number to an Excel column title, we can use a simple algorithm by repeatedly dividing the column number by 26 (the number of letters in the English alphabet) and converting the remainders to corresponding characters.

  1. Initialize an empty string to store the result.
  2. While the column number is greater than 0:
    • Calculate the remainder of the column number divided by 26.
    • Adjust the remainder to be in the range of 1 to 26.
    • Convert the remainder to the corresponding character ('A' to 'Z') and append it to the result string.
    • Update the column number by subtracting the remainder and dividing by 26.
  3. Reverse the result string to get the correct order of characters.
  4. Return the result string as the Excel column title.

Time Complexity: O(log n) where n is the column number, as we are dividing the number by 26 in each iteration.

Space Complexity: O(log n) for storing the result string.

:

Solution Code

class Solution {
    public String convertToTitle(int columnNumber) {
        StringBuilder result = new StringBuilder();
        while (columnNumber > 0) {
            columnNumber--;
            result.insert(0, (char)('A' + columnNumber % 26));
            columnNumber /= 26;
        }
        return result.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 168 (Excel Sheet Column Title)?

This page provides optimized solutions for LeetCode problem 168 (Excel Sheet Column Title) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 168 (Excel Sheet Column Title)?

The time complexity for LeetCode 168 (Excel Sheet Column Title) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 168 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 168 in Java, C++, or Python.

Back to LeetCode Solutions