Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Roman Number to Integer

Easy Acceptance 43.21% Points 20.00

Given a string in Roman number format (s), your task is to convert it to an integer. Various symbols and their values are given below.

Note: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000

Examples
Example 1

Example 1

Input: s = "CDXLIV"

Output: 444

Explanation: CD = 400, XL = 40, IV = 4 ? Total = 400 + 40 + 4 = 444

Example 2

Example 2

Input: s = "DCCCXC"

Output: 890

Explanation: D = 500, C = 100, C = 100, C = 100, XC = 90 ? Total = 500 + 100 + 100 + 100 + 90 = 890

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1<= roman number <=3999
  • s[i] belongs to [I, V, X, L, C, D, M]
Companies
Amazon Microsoft Zoho
Topics
String
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions