LeetCode Problem Workspace

Excel Sheet Column Number

This problem requires converting an Excel column title into its corresponding column number by applying a math and string manipulation pattern.

category

2

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Math plus String

bolt

Answer-first summary

This problem requires converting an Excel column title into its corresponding column number by applying a math and string manipulation pattern.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus String

Try AiBox Copilotarrow_forward

To solve the problem of converting Excel column titles to column numbers, we can treat the title as a base-26 number. Each letter corresponds to a value, where 'A' is 1, 'B' is 2, and so on. The solution involves iterating through each letter and updating the column number based on the place value, much like converting a string from base-26 to decimal.

Problem Statement

Given a string columnTitle representing an Excel column title, your task is to return the corresponding column number. The column titles are based on a modified base-26 number system where 'A' equals 1, 'B' equals 2, and so on. The column title can be up to 7 characters long and consists only of uppercase letters.

For example, 'A' is 1, 'B' is 2, and 'Z' is 26. After 'Z', the titles continue with 'AA', 'AB', and so on. You must find the numeric value for any given column title in this system.

Examples

Example 1

Input: See original problem statement.

Output: See original problem statement.

A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...

Example 2

Input: columnTitle = "A"

Output: 1

Example details omitted.

Example 3

Input: columnTitle = "AB"

Output: 28

Example details omitted.

Constraints

  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].

Solution Approach

Base-26 Conversion

The key to solving this problem is treating the column title as a base-26 number. Starting from the leftmost letter, calculate its corresponding value ('A' = 1, 'B' = 2, etc.) and adjust for the place value by multiplying the running total by 26 and adding the current letter's value.

Iterative Calculation

Iterate through the string from left to right. For each character, convert it to its corresponding value, adjust for the place value (base-26), and accumulate the result. This process will give you the final column number.

Edge Cases

Consider edge cases where the column title is a single letter (e.g., 'A'), as well as when the column title is the highest possible (e.g., 'FXSHRXW'). Ensure your solution can handle the full range of possible column titles.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity is O(n), where n is the length of the input string, because we iterate through each character once. The space complexity is O(1) since we're only storing a few variables to track the result during iteration, irrespective of the input size.

What Interviewers Usually Probe

  • Look for a candidate who uses a mathematical approach to solve the problem, rather than brute-forcing through all possible values.
  • The ability to handle the base-26 conversion correctly is key to solving the problem efficiently.
  • Check if the candidate can identify and explain potential edge cases like the shortest and longest column titles.

Common Pitfalls or Variants

Common pitfalls

  • Candidates may fail to account for the place value when calculating the column number, leading to incorrect results.
  • Some may treat the problem like a simple string manipulation problem rather than recognizing the base-26 nature of the column title.
  • Misunderstanding the range of column titles, especially with long titles like 'FXSHRXW', could lead to performance issues.

Follow-up variants

  • What happens if the column title is in lowercase? (Adjust for case sensitivity.)
  • Can this approach be optimized for large strings if we have a maximum length greater than 7 characters?
  • How would the problem change if the column titles were in a different base, like base-10?

FAQ

What is the main concept behind solving the 'Excel Sheet Column Number' problem?

The problem is solved using a base-26 number system, where each letter corresponds to a value ('A' = 1, 'B' = 2, etc.), and the result is computed iteratively by adjusting for place value.

How do I handle the edge case of the shortest column title, 'A'?

Simply return 1 since 'A' corresponds to the first column in Excel.

Can this solution be optimized for very large column titles?

The solution already operates in O(n) time, where n is the length of the string. There is no need for further optimization unless the constraints change significantly.

What are the constraints for this problem?

The column title can be between 1 and 7 characters long, consisting only of uppercase letters, and the title is in the range from 'A' to 'FXSHRXW'.

Why is the time complexity O(n) for this problem?

The time complexity is O(n) because we process each character of the input string exactly once, performing a constant amount of work for each character.

terminal

Solution

Solution 1: Base Conversion

The column name in Excel is a representation in base 26. For example, "AB" represents the column number $1 \times 26 + 2 = 28$.

1
2
3
4
5
6
class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        ans = 0
        for c in map(ord, columnTitle):
            ans = ans * 26 + c - ord("A") + 1
        return ans
Excel Sheet Column Number Solution: Math plus String | LeetCode #171 Easy