Hone logo
Hone
Problems

Excel Sheet Column Title Conversion

Excel spreadsheets use a clever system to label their columns, starting with 'A' for the first column, 'B' for the second, and so on. When the alphabet runs out, it combines letters, like 'AA' for the 27th column, 'AB' for the 28th, and 'AZ' for the 52nd, followed by 'BA' for the 53rd. Your task is to create a function that converts a given positive integer into its corresponding Excel column title. This is a common problem in data manipulation and understanding spreadsheet structures.

Problem Description

You need to implement a function that takes a positive integer n as input and returns its corresponding Excel column title as a string. The conversion follows a base-26 system, but with a twist: instead of digits 0-25, it uses letters 'A'-'Z', where 'A' represents 1, 'B' represents 2, ..., and 'Z' represents 26.

Key Requirements:

  • The function must accept a single positive integer n.
  • The function must return a string representing the Excel column title.
  • The conversion should accurately reflect the Excel column labeling system.

Expected Behavior:

  • For n = 1, the output should be "A".
  • For n = 26, the output should be "Z".
  • For n = 27, the output should be "AA".
  • For n = 28, the output should be "AB".
  • For n = 701, the output should be "ZY".

Edge Cases to Consider:

  • The input n is always a positive integer.

Examples

Example 1:

Input: 1
Output: "A"
Explanation: The first column is labeled 'A'.

Example 2:

Input: 26
Output: "Z"
Explanation: The 26th column is labeled 'Z'.

Example 3:

Input: 27
Output: "AA"
Explanation: After 'Z' (26), the next column starts a new sequence with 'A' preceding it, making it 'AA'.

Example 4:

Input: 701
Output: "ZY"
Explanation:
- 701 divided by 26 is 26 with a remainder of 25.
- The remainder 25 corresponds to 'Y'.
- The quotient 26 needs to be processed further.
- 26 divided by 26 is 1 with a remainder of 0.
- In this system, a remainder of 0 is treated as 26 ('Z') and the quotient is decremented. So, 26 becomes 25, which corresponds to 'Z'.
- Combining the letters in reverse order of calculation: 'Z' then 'Y' gives "ZY".

Constraints

  • 1 <= n <= 2^31 - 1 (standard 32-bit signed integer range)
  • The input n will always be a positive integer.
  • The generated column title will fit within a reasonable string length (e.g., less than 100 characters).
  • The solution should be efficient, aiming for a time complexity related to the number of digits in the output string (logarithmic to n).

Notes

This problem can be solved using a variation of base conversion. Think about how you would convert a number to base-10, base-2, or base-16, and adapt that logic to a base-26 system. Pay close attention to how remainders are handled, especially when they would correspond to a '0' in a standard base system. Remember that Excel's system starts from 1 ('A') rather than 0.

Loading editor...
plaintext