DevPath

← All problems

Excel Column Title

Easy math · strings

Description

Convert a positive integer to its Excel column title.

Constraints

1 <= n <= 10^7

Examples

Input: n = 28

Output: "AB"

Hints (1)
  • Decrement before taking the remainder so 26 maps to Z.

Solutions

Base-26 with decrement. · Time O(log n) · Space O(1)

func convertToTitle(n int) string {
    result := ""
    for n > 0 {
        n--
        result = string(rune('A'+n%26)) + result
        n /= 26
    }
    return result
}

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

Create account