4. Roman Number to Integer
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To convert a Roman numeral to an integer,
We can iterate through the input string from left to right.
If the current character represents a value less than the next character, we subtract the current value from the result; otherwise, we add it to the result. By doing this, we handle for cases like IV (4) and IX (9), where a smaller value precedes a larger value.
Time Complexity: The time complexity of this algorithm is O(n)
, where n
is the length of the input string str
. We iterate through the string once.
Auxiliary Space Complexity: The auxiliary space complexity is O(1)
because we use a fixed-size unordered_map and a few integer variables regardless of the input size.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.