Minimum Hidden Roman Value
Updated
Problem statement
You are given a string text. Keep only the Roman numeral letters I, V, X, L, C, D, and M, in the order they appear. Drop every other character.
Then read the kept letters from left to right. At each step, either take one letter at its usual Roman value, or take the next two kept letters together as one pair. A pair is allowed only if it is IV, IX, XL, XC, CD, or CM. A pair is worth the second letter's value minus the first letter's value.
You choose which allowed pairs to join. Return the smallest total you can get.
Function
minimumHiddenRomanValue(text: String) → intExamples
Example 1
text = "I HAVE EATEN"return = 4The kept Roman letters are IV. Taking them one at a time would give six. Joining them as the pair IV gives 4, which is smaller.
Example 2
text = "XIV"return = 14Take X as 10 and join IV as 4, for a total of 14.
Constraints
textmay contain any uppercase letters and spaces.- Only
I,V,X,L,C,D, andMare kept.