diff --git a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.cpp b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.cpp index 328ecdf..985c5a2 100644 --- a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.cpp +++ b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.cpp @@ -1,3 +1,10 @@ +#include +#include +#include +#include + +using namespace std; + class Solution { public: int romanToInt(string s) { @@ -7,6 +14,9 @@ class Solution { int i = 0; while (i < n) { + if (i > 1 && d[s[i - 2]] < d[s[i - 1]] && d[s[i]] <= d[s[i - 1]]) { + throw invalid_argument("Invalid Roman numeric sequence " + s); + } if (i < n - 1 && d[s[i]] < d[s[i + 1]]) { summ += d[s[i + 1]] - d[s[i]]; i += 2; @@ -19,3 +29,10 @@ class Solution { return summ; } }; + +int main() { + Solution sol; + // cout << sol.romanToInt("IIXX") << endl; // Throws invalid_argument + // cout << sol.romanToInt("MMD") << endl; // Output: 2500 + return 0; +} diff --git a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.java b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.java index 6d7f9b1..f32c45c 100644 --- a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.java +++ b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.java @@ -17,6 +17,9 @@ public int romanToInt(String s) { int i = 0; while (i < n) { + if (i > 1 && d.get(s.charAt(i - 2)) < d.get(s.charAt(i - 1)) && d.get(s.charAt(i)) <= d.get(s.charAt(i - 1))) { + throw new IllegalArgumentException("Invalid Roman numeric sequence " + s); + } if (i < n - 1 && d.get(s.charAt(i)) < d.get(s.charAt(i + 1))) { summ += d.get(s.charAt(i + 1)) - d.get(s.charAt(i)); i += 2; @@ -28,4 +31,10 @@ public int romanToInt(String s) { return summ; } + + public static void main(String[] args) { + Solution sol = new Solution(); + // System.out.println(sol.romanToInt("IIXX")); // Throws IllegalArgumentException + // System.out.println(sol.romanToInt("MMD")); // Output: 2500 + } } diff --git a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.js b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.js index f1f97e2..564bb47 100644 --- a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.js +++ b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.js @@ -5,6 +5,9 @@ function romanToInt(s) { let i = 0; while (i < n) { + if (i > 1 && d[s[i - 2]] < d[s[i - 1]] && d[s[i]] <= d[s[i - 1]]) { + throw new Error(`Invalid Roman numeric sequence ${s}`); + } if (i < n - 1 && d[s[i]] < d[s[i + 1]]) { summ += d[s[i + 1]] - d[s[i]]; i += 2; @@ -16,3 +19,7 @@ function romanToInt(s) { return summ; } + +// Example Test Cases +// console.log(romanToInt('IIXX')); // Throws Error +// console.log(romanToInt('MMD')); // Output: 2500 diff --git a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.py b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.py index b4e0f48..dc63dd4 100644 --- a/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.py +++ b/Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.py @@ -6,6 +6,9 @@ def romanToInt(self, s: str) -> int: i = 0 while i < n: + if i > 1 and d[s[i - 2]] < d[s[i - 1]] and d[s[i]] <= d[s[i - 1]]: + raise ValueError(f"Invalid Roman numeric sequence {s}") + if i < n - 1 and d[s[i]] < d[s[i+1]]: summ += d[s[i+1]] - d[s[i]] i += 2 @@ -16,3 +19,8 @@ def romanToInt(self, s: str) -> int: return summ # Time: O(n) # Space: O(1) + +# Example Test Cases +# sol = Solution() +# print(sol.romanToInt('IIXX')) # Raises ValueError +# print(sol.romanToInt('MMD')) # Output: 2500