Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 1.05 KB

column-name-from-a-given-column-number.md

File metadata and controls

39 lines (30 loc) · 1.05 KB

Column name from a given column number

Problem Link

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA” and so on.

Sample Input

28

Sample Output

AB

Solution

class Solution{
    public:
    string colName (long long int n)
    {
        string ans;

        while(n) {
            ans += (char) ('A' + (n-1) % 26);
            n = (n-1)/26;
        }
        
        reverse(ans.begin(), ans.end());
        
        return ans;
    }
};

Accepted

image