Roman to Integer

17 February 2025 | Viewed 175 times

C# Code
public class Solution {
public int RomanToInteger(string s) {
int num=0;
Dictionary<string, int> map = new Dictionary<string, int>(){
{"M",1000},
{"CM", 900},
{"D",500},
{"CD",400},
{"C",100},
{"XC",90},
{"L",50},
{"XL",40},
{"X",10},
{"IX",9},
{"V",5},
{"IV",4},
{"I",1}
};

foreach(var kvp in map)
{
while(s.StartsWith(kvp.Key))
{
num +=kvp.Value;
s = s.Remove(0, kvp.Key.Length);
}
}
return num;
}
}


Next