private bool checkSubstract2(string s) { int length = s.Length; if (length <= 2) { return(true); } else { // Check substring of length 3 for (int i = 2; i < length; i++) { RomanSymbol current = new RomanSymbol(s[i]); RomanSymbol currentMinus1 = new RomanSymbol(s[i - 1]); RomanSymbol currentMinus2 = new RomanSymbol(s[i - 2]); if (current.GetValue() > currentMinus1.GetValue() && current.GetValue() > currentMinus2.GetValue()) { return(false); } } } return(true); }
private bool checkSubstractPattern(string s) { // Rules: // "I" can be subtracted from "V" and "X" only // "X" can be subtracted from "L" and "C" only. // "C" can be subtracted from "D" and "M" only. // "V", "L", and "D" can never be subtracted. char[] input = s.ToArray(); char[] substractable = { 'I', 'X', 'C' }; for (int i = 0; i < s.Length - 1; i++) { char current = input[i]; RomanSymbol currentS = new RomanSymbol(current); char next = input[i + 1]; RomanSymbol nextS = new RomanSymbol(next); if (substractable.Contains(current)) { // small values can only be substracted from values <= 10 times of itself if (nextS.GetValue() > currentS.GetValue() * 10) { return(false); } } else { // "V", "L", and "D" can never be subtracted. if (currentS.GetValue() < nextS.GetValue()) { return(false); } } } return(true); }