Excel Column Title
Easy math · strings
Description
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
}
Base-26 with decrement. · Time O(log n) · Space O(1)
function convertToTitle(n) {
let result = '';
while (n > 0) {
n--;
result = String.fromCharCode(65 + (n % 26)) + result;
n = Math.floor(n / 26);
}
return result;
}
Base-26 with decrement. · Time O(log n) · Space O(1)
function convertToTitle($n) {
$result = '';
while ($n > 0) {
$n--;
$result = chr(65 + $n % 26).$result;
$n = intdiv($n, 26);
}
return $result;
}
Base-26 with decrement. · Time O(log n) · Space O(1)
def convertToTitle(n):
result = ""
while n > 0:
n -= 1
result = chr(65 + n % 26) + result
n //= 26
return result
Base-26 with decrement. · Time O(log n) · Space O(1)
function convertToTitle(n: number): string {
let result = '';
while (n > 0) {
n--;
result = String.fromCharCode(65 + (n % 26)) + result;
n = Math.floor(n / 26);
}
return result;
}
Create a free account to submit solutions, track progress and save problems.
Create account