Problem
Minimum Hidden Roman Value
Learn this problemProblem statement
You are given a string text. Extract only the Roman numeral letters I, V, X, L, C, D, and M, preserving their original order.
After extraction, parse the letters from left to right. You may either take one letter as its normal Roman value, or combine two adjacent extracted letters as one subtractive pair if the pair is one of IV, IX, XL, XC, CD, or CM. A combined pair contributes the second value minus the first value.
Return the minimum total value obtainable by choosing which adjacent valid subtractive pairs to combine.
Function
minimumHiddenRomanValue(text: String) → intExamples
Example 1
text = "I HAVE EATEN"return = 4The extracted Roman letters are IV. Combining them as the subtractive pair IV gives value 4.
Example 2
text = "XIV"return = 14Take X as 10 and combine IV as 4, for a total of 14.
Constraints
textmay contain arbitrary uppercase letters and spaces.- Only
I,V,X,L,C,D, andMare used after extraction.