Excel Sheet Column Number
Excel uses a unique numbering system for its columns. Instead of just using numbers, it uses letters. 'A' represents column 1, 'B' represents column 2, and so on, up to 'Z' representing column 26. Once it reaches 'Z', it continues with 'AA' (27), 'AB' (28), and so on. This system is useful for referencing cells within large spreadsheets. Your task is to write a function that converts an Excel column title string into its corresponding column number.
Problem Description
You are given a string columnTitle that represents an Excel column title. Your goal is to convert this string into its corresponding integer column number. The conversion follows a base-26 system where 'A' maps to 1, 'B' maps to 2, ..., 'Z' maps to 26.
Key Requirements:
- The input
columnTitlewill be a non-empty string containing only uppercase English letters. - The output should be a positive integer.
Expected Behavior:
- Single-letter titles should map to their corresponding alphabetical position (e.g., "A" -> 1, "Z" -> 26).
- Multi-letter titles should be interpreted as a base-26 number system. For instance, "AB" means (1 * 26^1) + (2 * 26^0) = 26 + 2 = 28. "ZY" means (26 * 26^1) + (25 * 26^0) = 676 + 25 = 701.
Edge Cases to Consider:
- The smallest possible input is "A".
- The largest possible input is not explicitly defined by a maximum length, but consider how your solution scales.
Examples
Example 1:
Input: "A"
Output: 1
Explanation: "A" is the first letter of the alphabet, so it corresponds to column 1.
Example 2:
Input: "AB"
Output: 28
Explanation: "A" represents 1, and "B" represents 2. In base-26, this is (1 * 26^1) + (2 * 26^0) = 26 + 2 = 28.
Example 3:
Input: "ZY"
Output: 701
Explanation: "Z" represents 26, and "Y" represents 25. In base-26, this is (26 * 26^1) + (25 * 26^0) = 676 + 25 = 701.
Example 4:
Input: "AAA"
Output: 703
Explanation: "A" represents 1. In base-26, this is (1 * 26^2) + (1 * 26^1) + (1 * 26^0) = 676 + 26 + 1 = 703.
Constraints
- 1 <=
columnTitle.length<= 7 (This is a typical constraint found in competitive programming platforms for this problem, ensuring numbers don't overflow standard integer types and keeping complexity manageable. If you encounter issues with larger inputs, consider using larger integer types if available in your chosen language.) columnTitleconsists only of uppercase English letters.- The returned integer will fit in a 32-bit signed integer.
Notes
- Think about how you can map each letter to its corresponding numerical value (1-26).
- Consider the positional value of each letter, similar to how you would convert a number from a different base (like binary or hexadecimal) to base-10.
- A loop iterating through the string from left to right might be a good starting point.