Roman to Integer - Complete Solution Guide
Roman to Integer is LeetCode problem 13, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks you to convert a Roman numeral string into its corresponding integer value. Roman numerals use a combination of seven symbols (I, V, X, L, C, D, M) representing 1, 5, 10, 50, 100, 500, and 1000 respectively. While typically written largest to smallest, subtractive notation is used for certain values (e.g., IV = 4, IX = 9). The input is a string representing a valid Roman numeral (between 1 and 3999), and the output is its integer equivalent.
Solution Approach
The provided Python and Java solutions use a hash map to store the integer values of each Roman numeral. They then iterate through the string, comparing the current symbol's value with the next one. If the current value is less than the next, it's a subtractive case, and the current value is subtracted; otherwise, it's added. The C++ solution uses a series of `if-else if` statements to achieve similar functionality. The C solution takes a more brute force approach by explicitly checking for all subtractive combinations.
Step-by-Step Algorithm
- Step 1: Create a hash map (or equivalent) to store the integer value of each Roman numeral symbol.
- Step 2: (Python/Java) Iterate through the input string from left to right (or right to left in Java).
- Step 3: (Python/Java) Compare the value of the current symbol with the next symbol's value (if it exists). If the current value is smaller, subtract it from the result; otherwise, add it.
- Step 4: (C++) Check each character and apply subtraction rule based on the next character if necessary.
- Step 5: (C) Explicitly checks for all possible subtractive combinations.
- Step 6: Return the final accumulated integer value.
Key Insights
- Insight 1: Recognizing the subtractive notation is crucial. Simply summing the values of each symbol will be incorrect for cases like IV or IX.
- Insight 2: A hash map (or similar lookup structure) is efficient for quickly obtaining the integer value of each Roman numeral symbol.
- Insight 3: Iterating either from left-to-right or right-to-left can simplify the logic for handling subtractive notation. Right-to-left avoids the need to look ahead.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, Math, String.
Companies
Asked at: AMD, Accenture, Adobe, Amazon, Apple, Axon, BNY Mellon, Bloomberg, Booking.com, Capital One, DeltaX, Expedia, Geico, Goldman Sachs, IBM, Infosys, KLA, Meta, Microsoft, Oracle, Pwc, Salesforce, Snowflake, SoFi, Thomson Reuters, Uber, Visa, Warnermedia, Wipro, Wise, Wix, Yahoo, Zoho, eBay, tcs.