Roman to Integer
Array, Iterative Approach
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
For example, 2
is written as II
in Roman numeral, just two ones added together. 12
is written as XII
, which is simply X + II
. The number 27
is written as XXVII
, which is XX + V + II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= s.length <= 15
s
contains only the characters('I', 'V', 'X', 'L', 'C', 'D', 'M')
.It is guaranteed that
s
is a valid roman numeral in the range[1, 3999]
.
Solutions
The problem is to convert a Roman numeral to an integer. The solution involves a process of iterating over the Roman numeral from left to right. For each character, it adds its corresponding integer value to the result. If a character represents a value that is less than the next character’s value, it subtracts this value instead of adding it. The final result is the integer representation of the original Roman numeral.
Approach - Brute Force Technique
Checks each character in the string one by one.
Steps
Dictionary Initialization: A dictionary
romanLatters
is initialized with Roman numerals as keys and their corresponding integer values as values.Input Validation: The
IsValidInput
function checks if the input strings
is within the valid length (1 to 15 characters) and if all characters ins
are valid Roman numerals. If the input string is invalid, the function returnsfalse
, and theRomanToInt
function will return zero.Roman Numeral Conversion: The
RomanToInt
function converts the input Roman numeral to an integer. It initializes an integeroutput
to store the result and a variablepre
to keep track of the value of the previous Roman numeral.Iteration Over String: The function iterates over each character in the input string
s
. For each character, it gets the corresponding integer valuecurrent
from theromanLatters
dictionary.Addition and Subtraction: If
pre
is zero orpre
is greater than or equal tocurrent
, the function addscurrent
tooutput
. Otherwise, it subtractspre
fromoutput
and addscurrent - pre
tooutput
.Update Previous Value: After processing each character, the function updates
pre
tocurrent
.Completion: The function continues this process until all characters in the string have been processed. At this point,
output
is the integer equivalent of the input Roman numeral.Return Result: Finally, the function returns the integer
output
.
Complexity
Time Complexity: O(1)
Auxiliary Space: O(1)
Time Complexity: The time complexity is O(1). This is because the max length of string is 15 characters.
Space Complexity: The space complexity is O(1). The space used by the program does not grow with the size of the input string. The dictionary of Roman numerals uses a constant amount of space, and the integer variables output
and pre
also use a constant amount of space.
Please note that this analysis assumes that dictionary lookups can be done in constant time.
Approach - Iterative Technique
The solution involves a process of iterating over the Roman numeral from left to right. For each character, it checks if it represents a value that is less than the next character’s value. If it is, it subtracts this value from the result. Otherwise, it adds this value to the result. The final result is the integer representation of the original Roman numeral.
Steps
Dictionary Initialization: A dictionary
romanLatters
is initialized with Roman numerals as keys and their corresponding integer values as values.Input Validation: The
IsValidInput
function checks if the input strings
is within the valid length (1 to 15 characters) and if all characters ins
are valid Roman numerals. If the input string is invalid, the function returnsfalse
, and theRomanToInt
function will return zero.Roman Numeral Conversion: The
RomanToInt
function converts the input Roman numeral to an integer. It initializes an integeroutput
to store the result.Iteration Over String: The function iterates over each character in the input string
s
using afor
loop. For each character, it gets the corresponding integer valuecurrent
from theromanLatters
dictionary.Addition and Subtraction: Inside the
for
loop, it checks if the current character’s value is less than the next character’s value. If it is, it subtractscurrent
fromoutput
. Otherwise, it addscurrent
tooutput
.Completion: The function continues this process until all characters in the string have been processed. At this point,
output
is the integer equivalent of the input Roman numeral.Return Result: Finally, the function returns the integer
output
.
Complexity
Time Complexity: O(1)
Auxiliary Space: O(1)
Time Complexity: The time complexity is O(1). This is because the max length of string is 15 characters.
Space Complexity: The space complexity is O(1). The space used by the program does not grow with the size of the input string. The dictionary of Roman numerals uses a constant amount of space, and the integer variables output
and pre
also use a constant amount of space.
Please note that this analysis assumes that dictionary lookups can be done in constant time.
Last updated