Posts Excel Column Number (InterviewBit)
Post
Cancel

Excel Column Number (InterviewBit)

PROBLEM DESCRIPTION

Given a column title A as appears in an Excel sheet, return its corresponding column number.

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Solution {

    public int titleToNumber(String A) {

        int res = 0;
        int n = A.length();

        // Iterate through each character in reverse order
        for(int i=n-1; i>=0; i--){

            // Extract the current character
            char ch = A.charAt(i);

            // Calculate the numeric value of the character (A=1, B=2, C=3 etc.)
            int chVal = ch - 'A' + 1;

            // Calculate the power of 26 based on the position of the character in the title
            int pow = n-i-1;

            // Update the result by adding the product of character value and 26 raised to the power
            res = res + chVal * (int) Math.pow(26, pow);

        }

        return res;

    }

}
This post is licensed under CC BY 4.0 by the author.