Problem
Minimum Hidden Roman Value
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.
Examples
01 · Example 1
text = "I HAVE EATEN" return = 4
The extracted Roman letters are IV. Combining them as the subtractive pair IV gives value 4.
02 · Example 2
text = "XIV" return = 14
Take 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.
More Zopsmart problems
public int minimumHiddenRomanValue(String text) {
// write your code here
}text"I HAVE EATEN"
expected4
sign in to submit