4. Roman Number to Integer
The problem can be found at the following link: Question Link
My Approach
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 and Auxiliary Space Complexity
Time Complexity: The time complexity of this algorithm is
O(n)
, wheren
is the length of the input stringstr
. 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.
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated