본문 바로가기

Algorithm/Problems

[LeetCode] 13. Roman to Integer

1. Description

Roman numericals 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 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. Intead, 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 size 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.

 

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].

 

2. Algorithms

Using dictionary, we can get designated integer with O(1) time complexity. We will store all roman numeral with corresponding integer in to dictionary.

 

By iterating through the character, we will return corresponding integer.

 

  1. Start
  2. Declare variable named rom2int
  3. Initialize roman2int with key(roman numeral) : value(integer) pairs
  4. Declare variable named res
  5. Initialize res to 0
  6. Repeat the steps through the index and character of input string s
    1. Increment res by corresponding integer of character
    2. If index is smaller than len(s) - 1 and,
      1. If s[i] = 'I' and s[i+1] = 'V' or 'X', Increment res by -2
      2. If s[i] = 'X' and s[i+1] = 'L' or 'C', Increment res by -20
      3. If s[i] = 'C' and s[i+1] = 'D' or 'M', increment res by -200
  7. Return the res
  8. End

 

3. Codes

class Solution:
    def romanToInt(self, s: str) -> int:
        # Hash map for roman numeral and corresponding integer 
        rom2int = {"I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000}

        res = 0 
        for i, c in enumerate(s): 
            res += rom2int[c] 

            if i < len(s) - 1: 
                if (c == 'I') and (s[i+1] == 'V' or s[i+1] == 'X'): 
                    res -= 2
                if (c == 'X') and (s[i+1] == 'L' or s[i+1] == 'C'): 
                    res -= 20 
                if (c == 'C') and (s[i+1] == 'D' or s[i+1] == 'M'): 
                    res -= 200 
        
        return res

 

4. Conclusion

 

Time Complexity : \(O(N)\)

Space Complexity : \(O(N)\)

'Algorithm > Problems' 카테고리의 다른 글

[LeetCode] 20. Valid Parentheses  (0) 2022.10.14
[LeetCode] 14. Longest Common Prefix  (0) 2022.10.14
[LeetCode] 9. Palindrome Number  (0) 2022.10.13
[LeetCode] 1. Two Sum  (0) 2022.10.13