This looks like a base-26 conversion: take a number, convert it to base 26, and map each digit to a letter. The complication is that the numbering does not start at zero.
Standard number systems are 0-indexed. In base 10, digits go from 0 to 9. In base 26, you would expect digits from 0 to 25. Excel columns are 1-indexed: they go from A (1) to Z (26), with no digit representing 0. There is no "zero" column.
So a plain columnNumber % 26 is wrong. When columnNumber is a multiple of 26 (like 26, 52, or 702), the modulo gives 0, but the correct letter is Z, which represents 26. The fix is to subtract 1 before each modulo operation, shifting from 1-indexed to 0-indexed at each step. (columnNumber - 1) % 26 produces a value from 0 to 25, which maps to A through Z.
1 <= columnNumber <= 2^31 - 1 -> The number can be up to about 2.1 billion. Any approach that processes one digit at a time runs in O(log_26(n)) time, which is at most about 7 iterations.To convert a number to a different base, repeatedly take the remainder when dividing by the base, then divide the number by the base. For base 10, n % 10 gives the last digit, then n = n / 10 moves to the next digit.
Excel columns work the same way with base 26, plus the 1-indexing adjustment. Before taking the modulo, subtract 1 from the number. This converts the 1 to 26 range into 0 to 25, which maps to A through Z. After getting the remainder, divide the adjusted number by 26 and repeat until the number reaches 0.
Since we extract digits from the least significant position first (rightmost character), we build the result in reverse and flip it at the end.
columnNumber is greater than 0:columnNumber (shift from 1-indexed to 0-indexed).remainder = columnNumber % 26.remainder (0 maps to 'A', 1 maps to 'B', ..., 25 maps to 'Z').columnNumber = columnNumber / 26.columnNumber by 26, so the loop runs at most ceil(log_26(n)) times. For the maximum value of 2^31 - 1, that is about 7 iterations.The iterative approach builds the string backwards and reverses it at the end. Recursion can produce the characters in the correct order directly, without a separate reversal step.
Instead of building the string from the rightmost character and reversing, recursion can handle the higher-order digits first. Before appending the current character, recursively process the remaining quotient. Because the recursive call runs before the append, the higher-order characters land in the string ahead of the current one, so the result comes out in left-to-right order.
The recursion mirrors how place value works: the quotient columnNumber / 26 holds every digit except the last, and columnNumber % 26 is the last character. Computing the prefix first, then appending the last character, places each character in its correct position with no reversal.
columnNumber is 0, return an empty string.columnNumber.columnNumber / 26 (the "prefix" of the result).columnNumber % 26.columnNumber by 26. The recursion depth is ceil(log_26(n)), which is at most 7 for the given constraints.