Problem Number: 13 Difficulty: Easy Category: Hash Table, Math, String LeetCode Link: https://leetcode.com/problems/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 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan 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 <= 15scontains only the characters('I', 'V', 'X', 'L', 'C', 'D', 'M')- It is guaranteed that
sis a valid roman numeral in the range[1, 3999]
I used a Right-to-Left approach with a hash table to map Roman numerals to their values. The key insight is to process the string from right to left, checking for subtraction cases when a smaller value appears before a larger one.
Algorithm:
- Create a hash table mapping Roman symbols to their values
- Process the string from right to left
- For each character, check if it forms a subtraction case with the previous character
- If it's a subtraction case, subtract the smaller value from the larger one
- Otherwise, add the current character's value
- Return the total sum
The solution uses a right-to-left approach with hash table lookup. See the implementation in the solution file.
Key Points:
- Uses hash table for O(1) symbol-to-value lookup
- Processes string from right to left for easier subtraction handling
- Checks for all six subtraction cases explicitly
- Handles edge case for the first character
Time Complexity: O(n)
- We traverse the string once from right to left
- Hash table lookups are O(1)
- Total: O(n)
Space Complexity: O(1)
- Hash table has constant size (7 symbols)
- No additional space proportional to input size
-
Right-to-Left Processing: Processing from right to left makes it easier to handle subtraction cases.
-
Subtraction Rules: There are exactly six cases where subtraction occurs:
- IV (4), IX (9)
- XL (40), XC (90)
- CD (400), CM (900)
-
Hash Table Efficiency: Using a hash table provides O(1) lookup for symbol values.
-
No Invalid Cases: The problem guarantees valid Roman numerals, so we don't need extensive validation.
-
Character Pairs: Subtraction only occurs with specific character pairs, making the logic straightforward.
-
Edge Case Handling: The first character (rightmost) is always added, never subtracted.
-
Left-to-Right Processing: Initially might process left-to-right, making subtraction logic more complex.
-
Missing Subtraction Cases: Forgetting to handle all six subtraction cases.
-
Complex Logic: Overcomplicating the subtraction detection logic.
-
Edge Cases: Not properly handling the first character case.
- Integer to Roman (Problem 12): Reverse conversion from integer to Roman numeral
- Valid Parentheses (Problem 20): Similar pattern matching with symbols
- Decode Ways (Problem 91): Converting string representations to numbers
- Basic Calculator (Problem 224): More complex mathematical expression parsing
- Left-to-Right with Lookahead: Process left-to-right and look ahead for subtraction cases
- Stack-based: Use a stack to handle the conversion
- Recursive: Use recursion to process the string
- Wrong Direction: Processing left-to-right makes subtraction logic more complex.
- Missing Cases: Not handling all six subtraction cases.
- Complex Logic: Overcomplicating the subtraction detection.
- Edge Cases: Not handling the first character properly.
- Invalid Input: Assuming invalid Roman numerals need to be handled.
Note: This is a straightforward string processing problem that introduces the concept of symbol-to-value mapping and special case handling.