Problem
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, 2
is written as II
in Roman numeral, just two ones added together. 12
is written as XII
, which is simply X + II
. The number 27
is written as XXVII
, which is XX + V + II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = “III” Output: 3 Explanation: III = 3.
Example 2:
Input: s = “LVIII” Output: 58 Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = “MCMXCIV” Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s
contains only the characters('I', 'V', 'X', 'L', 'C', 'D', 'M')
.- It is guaranteed that
s
is a valid roman numeral in the range[1, 3999]
.
Solutions
Left to Right Pass - O(1) time - O(1) space
class Solution {
public:
int romanToInt(string s) {
map<char, int> values{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
{'C', 100}, {'D', 500}, {'M', 1000}};
int sum = 0;
int i = 0;
while (i < s.length()) {
char currentSymbol = s[i];
int currentValue = values[currentSymbol];
int nextValue = 0;
// If a next value exists
if (i + 1 < s.length()) {
char nextSymbol = s[i + 1];
nextValue = values[nextSymbol];
}
// If this is the subtractive case.
if (currentValue < nextValue) {
sum += (nextValue - currentValue);
i += 2;
}
// else this is NOT the subtractive case.
else {
sum += currentValue;
i += 1;
}
}
return sum;
}
};
As there is a finite set of roman numerals, the maximum number possible number can be 3999
, which in roman numerals is MMMCMXCIX
. As such the time complexity is O(1).
If roman numerals had an arbitrary number of symbols, then the time complexity would be proportional to the length of the input, i.e. O(n). This is assuming that looking up the value of each symbol is O(1).
Improved Left to Right Pass - O(1) time - O(1) space
class Solution {
public:
static map<string, int>
values; // static ensures map initialization only happens once instead
// of every object of the class
Solution() {
if (values.empty()) {
values.insert(pair<string, int>("I", 1));
values.insert(pair<string, int>("V", 5));
values.insert(pair<string, int>("X", 10));
values.insert(pair<string, int>("L", 50));
values.insert(pair<string, int>("C", 100));
values.insert(pair<string, int>("D", 500));
values.insert(pair<string, int>("M", 1000));
values.insert(pair<string, int>("IV", 4));
values.insert(pair<string, int>("IX", 9));
values.insert(pair<string, int>("XL", 40));
values.insert(pair<string, int>("XC", 90));
values.insert(pair<string, int>("CD", 400));
values.insert(pair<string, int>("CM", 900));
}
}
int romanToInt(string s) {
int sum = 0;
int i = 0;
while (i < s.size()) {
if (i < s.size() - 1) {
string doubleSymbol = s.substr(i, 2);
// check if this is the length-2 symbol case
if (values.count(doubleSymbol)) {
sum += values[doubleSymbol];
i += 2;
continue;
}
}
// otherwise, it must be the length-1 symbol case
string singleSymbol = s.substr(i, 1);
sum += values[singleSymbol];
i += 1;
}
return sum;
}
};
map<string, int> Solution::values; // initalizing the map outside the class
Right to Left Pass - O(1) time - O(1) space
class Solution {
public:
static unordered_map<char, int> values;
int romanToInt(string s) {
char lastSymbol = s[s.size() - 1];
int lastValue = values[lastSymbol];
int total = lastValue;
for (int i = s.size() - 2; i >= 0; i--) {
char currentSymbol = s[i];
int currentValue = values[currentSymbol];
if (currentValue < lastValue) {
total -= currentValue;
} else {
total += currentValue;
}
lastValue = currentValue;
}
return total;
}
};
unordered_map<char, int> Solution::values = {
{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50},
{'X', 10}, {'V', 5}, {'I', 1}};
Left - O(1) time - O(1) space
using namespace std;
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> translations = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int total = 0;
for (int i = 0; i < s.size(); i++) {
char numeral = s[i];
if ((i > 0) &&
(
((numeral == 'V' || numeral == 'X') && s[i-1] == 'I') ||
((numeral == 'L' || numeral == 'C') && s[i-1] == 'X') ||
((numeral == 'D' || numeral == 'M') && s[i-1] == 'C')
)
) {
total -= translations[s[i-1]];
total += translations[numeral] - translations[s[i-1]];
} else {
total += translations[numeral];
}
}
return total;
}
};
Pattern Matching - O(n) time - O(1) space
using namespace std;
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> translations = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int total = 0;
char numeral;
for (int i = 0; i < s.size(); i++) {
numeral = s[i];
if (translations[s[i+1]] > translations[numeral]) {
total -= translations[numeral];
} else {
total += translations[numeral];
}
}
return total;
}
};