Excel Sheet Column Title
Convert a positive integer to its corresponding Excel column title.
Open on LeetCode ↗Intuition
This looks like plain base-26 conversion - take mod 26, take the letter, divide by 26, repeat - but that breaks because Excel's scheme is 1-indexed: there is no digit for zero, A is 1 and Z is 26, not 0 and 25. If you take a plain mod 26 on a number like 26 itself, you get remainder 0, which maps to 'A' with a leftover that produces 'AZ' instead of the correct single letter 'Z'. The fix is to subtract 1 from the current value BEFORE taking the modulo at every step, which shifts the range from 1..26 down to 0..25 just long enough to pick the right letter, then divide by 26 as usual.
Approach
Subtract 1 before every modulo
At each step, decrement the current value by 1 first. This remaps the 1-indexed range 1..26 to the 0-indexed range 0..25, so a plain modulo now lines up with letter indices A=0..Z=25 correctly.
Extract the letter and shrink the value
Take the decremented value mod 26 to get a digit 0..25, convert it to a letter with chr(65 + digit), and prepend it to the result. Then divide the decremented value by 26 (integer division) to move to the next higher place.
Repeat until the value reaches zero
Continue the subtract-mod-divide cycle while the current value is still positive; each iteration peels off one more letter from the least significant end, building the title back to front.
Solution & live demo
Edge cases
Subtracting 1 first gives 25, which mods to 25 -> 'Z', then divides to 0, stopping immediately - not 'AZ'.
Subtract 1 to get 26; 26 % 26 = 0 -> 'A', then 26 // 26 = 1, one more round gives 'A' again for the tens place -> 'AA'.
Subtract 1 to get 0; 0 % 26 = 0 -> 'A', divides to 0, loop ends -> 'A'.
Multiple rounds of subtract-mod-divide each contribute one letter, correctly producing multi-character titles like 'ZY'.