DevPath

← All problems

Excel Column Number

Easy math · strings

Description

Convert an Excel column title (like "AA") to its 1-based number.

Constraints

1 <= length <= 7; uppercase A-Z

Examples

Input: s = "AB"

Output: 28

Hints (1)
  • Treat it as base 26 where A is 1.

Solutions

Base-26 accumulation. · Time O(n) · Space O(1)

func titleToNumber(s string) int {
    result := 0
    for i := 0; i < len(s); i++ {
        result = result*26 + int(s[i]-'A'+1)
    }
    return result
}

Create a free account to submit solutions, track progress and save problems.

Create account