Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

48. Roman to Integer

EasyDay 15 of 60Strings

Roman numerals use the symbols I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Values are usually written from largest to smallest and added, but a smaller symbol placed before a larger one is subtracted (IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, CM = 900). Given a valid Roman numeral s, return its integer value.

Example 1
Input: s = "LVIII"
Output: 58
L = 50, V = 5, III = 3.
Example 2
Input: s = "MCMXCIV"
Output: 1994
M = 1000, CM = 900, XC = 90, IV = 4.
Constraints
  • 1 <= s.length <= 15
  • `s` is a valid Roman numeral in the range 1 to 3999
Hints
Frequently asked at: Amazon, Microsoft, Apple, TCS, Infosys
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min