-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathRomanToInteger13.java
124 lines (100 loc) · 2.44 KB
/
RomanToInteger13.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package MustDoEasyList;
import java.util.HashMap;
public class RomanToInteger13 {
public static int romanToInt(String s) {
HashMap<String, Integer> map = new HashMap<>();
initializeMap(map);
int len = s.length();
int idx = 0;
int sum = 0;
char ch1, ch2;
// parse string
while (idx < len) {
ch1 = ' ';
ch2 = ' ';
ch1 = s.charAt(idx);
if ((idx + 1) < len)
ch2 = s.charAt(idx + 1);
if (ch2 != ' ' && map.get("" + ch1) < map.get("" + ch2)) {
// if first character is small than next char
String currCombination = "" + ch1 + ch2;
sum += map.get(currCombination);
idx += 2; // two characters are combined so +2 in index
} else {
// considering only one Roman character
sum += map.get("" + ch1);
idx++;
}
}
return sum;
}
private static void initializeMap(HashMap<String, Integer> map) {
map.put("I", 1);
map.put("V", 5);
map.put("X", 10);
map.put("L", 50);
map.put("C", 100);
map.put("D", 500);
map.put("M", 1000);
// special cases
map.put("IV", 4);
map.put("IX", 9);
map.put("XL", 40);
map.put("XC", 90);
map.put("CD", 400);
map.put("CM", 900);
}
// optimal approach
public static int romanToInt2(String s) throws Exception {
int ans = 0;
int len = s.length();
for (int i = 0; i < len - 1; i++) {
int currValue = getRomanValue(s.charAt(i));
int nextValue = getRomanValue(s.charAt(i + 1));
if (currValue == -1 || nextValue == -1) {
throw new Exception("Invalid character found...");
}
if (currValue < nextValue) {
ans -= currValue;
} else {
ans += currValue;
}
}
int lastCharValue = getRomanValue(s.charAt(len - 1));
return ans + lastCharValue;
}
private static int getRomanValue(char next) {
switch (next) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
return -1;
}
public static void main(String[] args) throws Exception {
// System.out.println(romanToInt("III"));
// System.out.println(romanToInt("IV"));
// System.out.println(romanToInt("IX"));
// System.out.println(romanToInt("LVIII"));
// System.out.println(romanToInt("MCMXCIV"));
System.out.println(romanToInt2("MDCXCV"));
System.out.println(romanToInt2("IV"));
// System.out.println(getRomanValue('M'));
/*
* testing character concatenation
*
* System.out.println("" + 'c' + 'd');
*/
}
}