3. Column Name from a Given Column Number
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To convert a given column number to an Excel column name, I follow these steps:
Create a vector code
of characters to represent the alphabet, where code[i]
corresponds to the character for the column number i
(0 to 25).
Initialize the first element code[0]
as 'Z' because in Excel, the last column is 'Z'.
Iterate while n
is greater than 0:
Append the character represented by code[n % 26]
to the output string out
.
If n % 26
is 0, decrement n
by 1 to handle cases like column 26 ('Z').
Divide n
by 26 to move to the next column.
Reverse the out
string to get the correct column name.
Time Complexity: O(log(n))
, The loop runs for the number of digits in n
in base 26
, which is equivalent to O(log(n))
.
Auxiliary Space Complexity: O(log(n))
, The space required to store the output string out
is proportional to the number of digits in n
in base 26
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.