Problems / Roman to Integer
Easy

Roman to Integer

Easy 0 submissions 10 coins +50 XP
Hash Table Math String
Problem Description
## Problem Roman numerals are represented by seven different symbols: | Symbol | Value | |--------|-------| | I | 1 | | V | 5 | | X | 10 | | L | 50 | | C | 100 | | D | 500 | | M | 1000 | Roman numerals are usually written largest to smallest from left to right. However, there are six special subtractive cases: - `I` before `V` or `X` → 4 and 9 - `X` before `L` or `C` → 40 and 90 - `C` before `D` or `M` → 400 and 900 Given a roman numeral string `s`, convert it to an integer. ## Examples **Example 1:** ``` Input: s = "III" Output: 3 Explanation: III = 1 + 1 + 1 = 3. ``` **Example 2:** ``` Input: s = "LVIII" Output: 58 Explanation: L = 50, V = 5, III = 3. Total = 58. ``` **Example 3:** ``` Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90, IV = 4. Total = 1994. ``` ## Hints - Use a hash map to store symbol values. - Scan from left to right. If the current symbol is less than the next symbol, subtract it; otherwise add it.
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]`.

Need help?

Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.

Video Call Whiteboard Live Coding Screen Share