Roman to Integer

Medium Strings
Convert a Roman numeral to an integer. If a smaller value precedes a larger one, subtract it; otherwise add it.

Sample input

MCMXCIV

Sample output

1994

Solution

def roman_to_int(s):
    values = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    total = 0
    for i in range(len(s)):
        if i + 1 < len(s) and values[s[i]] < values[s[i + 1]]:
            total -= values[s[i]]
        else:
            total += values[s[i]]
    return total

print(roman_to_int("MCMXCIV"))
function romanToInt(s) {
  const values = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
  let total = 0;
  for (let i = 0; i < s.length; i++) {
    if (i + 1 < s.length && values[s[i]] < values[s[i + 1]]) {
      total -= values[s[i]];
    } else {
      total += values[s[i]];
    }
  }
  return total;
}

console.log(romanToInt("MCMXCIV"));
import java.util.Map;

public class Main {
    static int romanToInt(String s) {
        Map<Character, Integer> values = Map.of(
            'I', 1, 'V', 5, 'X', 10, 'L', 50, 'C', 100, 'D', 500, 'M', 1000);
        int total = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i + 1 < s.length() && values.get(s.charAt(i)) < values.get(s.charAt(i + 1))) {
                total -= values.get(s.charAt(i));
            } else {
                total += values.get(s.charAt(i));
            }
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("MCMXCIV"));
    }
}
fun romanToInt(s: String): Int {
    val values = mapOf('I' to 1, 'V' to 5, 'X' to 10, 'L' to 50, 'C' to 100, 'D' to 500, 'M' to 1000)
    var total = 0
    for (i in s.indices) {
        if (i + 1 < s.length && values.getValue(s[i]) < values.getValue(s[i + 1])) {
            total -= values.getValue(s[i])
        } else {
            total += values.getValue(s[i])
        }
    }
    return total
}

fun main() {
    println(romanToInt("MCMXCIV"))
}
func romanToInt(_ s: String) -> Int {
    let values: [Character: Int] = ["I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000]
    let chars = Array(s)
    var total = 0
    for i in 0..<chars.count {
        if i + 1 < chars.count && values[chars[i]]! < values[chars[i + 1]]! {
            total -= values[chars[i]]!
        } else {
            total += values[chars[i]]!
        }
    }
    return total
}

print(romanToInt("MCMXCIV"))
int romanToInt(String s) {
  final values = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000};
  int total = 0;
  for (var i = 0; i < s.length; i++) {
    if (i + 1 < s.length && values[s[i]]! < values[s[i + 1]]!) {
      total -= values[s[i]]!;
    } else {
      total += values[s[i]]!;
    }
  }
  return total;
}

void main() {
  print(romanToInt('MCMXCIV'));
}
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int romanToInt(const string& s) {
    unordered_map<char, int> values = {
        {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
    int total = 0;
    for (size_t i = 0; i < s.size(); i++) {
        if (i + 1 < s.size() && values[s[i]] < values[s[i + 1]]) {
            total -= values[s[i]];
        } else {
            total += values[s[i]];
        }
    }
    return total;
}

int main() {
    cout << romanToInt("MCMXCIV") << endl;
    return 0;
}
#include <stdio.h>
#include <string.h>

int value(char c) {
    switch (c) {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
    }
    return 0;
}

int main() {
    char s[] = "MCMXCIV";
    int n = strlen(s), total = 0;
    for (int i = 0; i < n; i++) {
        if (i + 1 < n && value(s[i]) < value(s[i + 1])) {
            total -= value(s[i]);
        } else {
            total += value(s[i]);
        }
    }
    printf("%d\n", total);
    return 0;
}