romanToInt function
Converts a Roman numeral string to its integer representation.
This function takes a Roman numeral s and returns its integer value.
Time Complexity: O(n), where n is the length of the string s.
Example:
print(romanToInt("MCMXCIV")); // Outputs: 1994
Implementation
int romanToInt(String s) {
final map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000};
int result = 0, prev = 0;
for (int i = s.length - 1; i >= 0; i--) {
int curr = map[s[i]]!;
if (curr < prev) {
result -= curr;
} else {
result += curr;
}
prev = curr;
}
return result;
}