ALGORITHM

Renzo Regio
3 min readMay 6, 2021

--

TIL 2021.05.05

Solved in: Javascript and Python

QUESTION

13. Roman to Integer

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 one’s 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 before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

Example 1:
Input: s = "III"
Output: 3

Example 2:
Input: s = "IV"
Output: 4

Example 3:
Input: s = "IX"
Output: 9

Example 4:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:
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].

Solution: JavaScript

/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
let num = 0
let arr = s.split("")
if(s.includes("IV") || s.includes("IX") || s.includes("XL") || s.includes("XC") || s.includes("CD") || s.includes("CM")){
for(let i = 0; i < arr.length; i++){
let first = arr[i]
for(let j = 1; j < arr.length; j++){
let second = arr[j]
if(first === "I" && second === "V" && i < j){
num += 4
delete arr[i]
delete arr[j]
} else if (first === "I" && second === "X" && i < j){
num += 9
delete arr[i]
delete arr[j]
} else if (first === "X" && second === "L" && i < j){
num += 40
delete arr[i]
delete arr[j]
}else if (first === "X" && second === "C" && i < j){
num += 90
delete arr[i]
delete arr[j]
}else if (first=== "C" && second === "D" && i < j){
num += 400
delete arr[i]
delete arr[j]
}else if (first === "C" && second === "M" && i < j){
num += 900
delete arr[i]
delete arr[j]
}
}
}
}

for(let i = 0; i < arr.length; i++){
if(arr[i] === "I"){
num +=1
} else if (arr[i] === "V"){
num += 5
} else if (arr[i] === "X"){
num += 10
} else if (arr[i] === "L"){
num += 50
} else if (arr[i] === "C"){
num += 100
} else if(arr[i] === "D"){
num += 500
} else if(arr[i] === "M"){
num += 1000
}
}

return num
};

Solution: Python

class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
new_list = []
total = 0
roman_list = list(s)
for count, letter in enumerate(s):
if count + 1 < len(s):
two_letters = letter, s[count + 1]
letters = ''.join(two_letters)
if letters == "CM":
total += 900
new_list.append("C")
new_list.append("M")
elif letters == "CD":
total += 400
new_list.append("C")
new_list.append("D")
elif letters == "XC":
total += 90
new_list.append("X")
new_list.append("C")
elif letters == "XL":
total += 40
new_list.append("X")
new_list.append("L")
elif letters == "IX":
total += 9
new_list.append("I")
new_list.append("X")
elif letters == "IV":
total += 4
new_list.append("I")
new_list.append("V")
else:
total += 0

for letter in new_list:
if letter in s:
roman_list.remove(letter)
for letter in roman_list:
if letter == "I":
total += 1
elif letter == "V":
total += 5
elif letter == "X":
total += 10
elif letter == "L":
total += 50
elif letter == "C":
total += 100
elif letter == "D":
total += 500
elif letter == "M":
total += 1000
return total

--

--

No responses yet