-
Notifications
You must be signed in to change notification settings - Fork 22
/
ExcelColumnNumbers.java
86 lines (78 loc) · 2.59 KB
/
ExcelColumnNumbers.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.interviewbit.math;
import com.util.LogUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* https://www.interviewbit.com/problems/excel-column-number/
* <p>
* Given a column title as appears in an Excel sheet, return its corresponding column number
* A -> 1
* <p>
* B -> 2
* <p>
* C -> 3
* <p>
* ...
* <p>
* Z -> 26
* <p>
* AA -> 27
* <p>
* AB -> 28
*
* @author neeraj on 2019-07-27
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class ExcelColumnNumbers {
static Map<Character, Integer> characterToNumberMapping = new HashMap<>();
static {
characterToNumberMapping.put('A', 1);
characterToNumberMapping.put('B', 2);
characterToNumberMapping.put('C', 3);
characterToNumberMapping.put('D', 4);
characterToNumberMapping.put('E', 5);
characterToNumberMapping.put('F', 6);
characterToNumberMapping.put('G', 7);
characterToNumberMapping.put('H', 8);
characterToNumberMapping.put('I', 9);
characterToNumberMapping.put('J', 10);
characterToNumberMapping.put('K', 11);
characterToNumberMapping.put('L', 12);
characterToNumberMapping.put('M', 13);
characterToNumberMapping.put('N', 14);
characterToNumberMapping.put('O', 15);
characterToNumberMapping.put('P', 16);
characterToNumberMapping.put('Q', 17);
characterToNumberMapping.put('R', 18);
characterToNumberMapping.put('S', 19);
characterToNumberMapping.put('T', 20);
characterToNumberMapping.put('U', 21);
characterToNumberMapping.put('V', 22);
characterToNumberMapping.put('W', 23);
characterToNumberMapping.put('X', 24);
characterToNumberMapping.put('Y', 25);
characterToNumberMapping.put('Z', 26);
}
public static void main(String[] args) {
List<String> inputs = Arrays.asList("A", "B", "C", "AB", "AZ", "ABD", "CZ", "WX", "DZ", "BAQUZ", "BAQTZ");
for (int i = 0; i < inputs.size(); i++) {
LogUtil.logIt(" Column Title " + inputs.get(i) + " to number is " + titleToNumber(inputs.get(i)));
}
}
/**
* The logic is simple, you have to do the base conversion, but this time the base is 26
*
* @param A
* @return
*/
public static int titleToNumber(String A) {
int finalResult = 0;
for (int i = 0; i < A.length(); i++) {
finalResult += Math.pow(26, i) * characterToNumberMapping.get(A.charAt(A.length() - 1 - i));
}
return finalResult;
}
}