impl Solution {
fn roman_to_int(s: String) -> i32 {
use std::collections::HashMap;
// Step 1: Define the mapping of Roman numerals to integers
let mut roman_values = HashMap::new();
roman_values.insert('I', 1);
roman_values.insert('V', 5);
roman_values.insert('X', 10);
roman_values.insert('L', 50);
roman_values.insert('C', 100);
roman_values.insert('D', 500);
roman_values.insert('M', 1000);
// Initialize a variable to store the result
let mut result = 0;
// Convert the input string to a vector of characters
let chars: Vec<char> = s.chars().collect();
// Iterate over the characters in the input string
for i in 0..chars.len() {
// If the current character is less than the next one, subtract its value from the result
if i + 1 < chars.len() && roman_values[&chars[i]] < roman_values[&chars[i + 1]] {
result -= roman_values[&chars[i]];
} else {
// Otherwise, add its value to the result
result += roman_values[&chars[i]];
}
}
// Return the result
result
}
}