Given a word, how to tell whether it is a Roman numeral?

136 Views Asked by At

If I have a word that consists of letters I, V, X, L, C, D, M, how can I tell whether it is a valid roman numeral? For example, how do I tell that IXXL is not valid?

1

There are 1 best solutions below

6
On BEST ANSWER

To ease the description you can consider the pairs "IV", "IX", "XL", "XC", "CD" and "CM" as single symbols.

That is, the roman numerals are sequences made of these symbols: I, IV, V, IX, X, XL, L, XC, C, CD, D, CM, M.

The sequences must hold these rules:

  • The symbols I, X, C, M can be repeated up to three consecutive times. Other symbols must not be repeated.
  • In each sequence, the symbols appear in decreasing order.
  • If a symbol with two letters occurs, none of these two letters occur after it, with the followinf exceptions: after XL and XC there can be IX; and after CD and CM there can be XC.

This way, your example is not legal because IX < XL, or because after IX there must not be any letter X.