Integer to Roman — Day 93(Python)

Photo by Hadi Yazdi Aznaveh on Unsplash

Today’s question is a medium-tagged question in Leetcode. This question is asked in multiple coding interviews. Let us look into the problem statement.

12. Integer to Roman

Roman numerals are represented by seven different symbols: , , , , , and .

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, is written as in Roman numeral, just two one's added together. is written as , which is simply . The number is written as , which is .

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not . Instead, the number four is written as . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as . There are six instances where subtraction is used:

  • can be placed before (5) and (10) to make 4 and 9.
  • can be placed before (50) and (100) to make 40 and 90.
  • can be placed before (500) and (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

Example 1:

Input: num = 3
Output: "III"

Example 2:

Input: num = 4
Output: "IV"

Example 3:

Input: num = 9
Output: "IX"

Example 4:

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

Example 5:

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

The very first thing that we need to do is define a dictionary that stores the definition of different roman symbols. Apart from defined symbols, we will specific cases number also.

d = {1000: 'M', 
900: 'CM',
500: 'D',
400: 'CD',
100: 'C',
90: 'XC',
50: 'L',
40: 'XL',
10: 'X',
9: 'IX',
5: 'V',
4: 'IV',
1: 'I'}

We need to make sure that the dictionary is defined in decreasing order.

Next, we will run a loop that starts from the first number in the dictionary. Check how many times the current input number is divisible by the dictionary key. This number will be the number of times the value of the respective dictionary key has to be repeated.

For eg: If the input number is 2000. 
We start our loop from 1000 in the dictionary. 2000 is divisible by 1000 twice. The value of 1000 in the dictionary is "M". Hence "M" will be repeated twice.

The roman numeral that we find in the above step will be stored in a result variable. Each time the loop is run, the input number is modulo by the current key in the dictionary.

Let us look into the code snippet.

class Solution:
def intToRoman(self, num: int) -> str:
d = {1000: 'M',
900: 'CM',
500: 'D',
400: 'CD',
100: 'C',
90: 'XC',
50: 'L',
40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}

res = ""

for i in d:
res += (num//i) * d[i]
num %= i
return res

Complexity analysis.

Time Complexity

The time complexity O(N) where is the number of keys in our dictionary. The number of keys in our dictionary depends on the range provided to us as a constraint for the input number.

Space Complexity

The space complexity is O(N) where N is the number of keys in our dictionary.

I had to look through the discussion board to understand how to solve this problem. The link to the discussion can be found here.

Software Engineer. Find me @ www.linkedin.com/in/annamariya-jt

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store