How about this:

$romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$roman = 'MMMCMXCIX';
$result = 0;

foreach ($romans as value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
echo $result;

which should output 3999 for the supplied $roman. It seems to work for my limited testing:

MCMXC = 1990
MM = 2000
MMXI = 2011
MCMLXXV = 1975

You might want to do some validation first as well :-)

Answer from andyb on Stack Overflow
🌐
LeetCode
leetcode.com › problems › roman-to-integer
Roman to Integer - LeetCode
Roman to Integer - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two ones added together.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › roman-number-to-integer
Roman to Integer Conversion - GeeksforGeeks
The idea for converting a Roman numeral to an integer is that, we have to traverse the string from left to right. For each symbol compare it with the next symbol (if it exists). If the current symbol is greater than or equal to the next symbol ...
Published: July 10, 2026
Discussions

Roman To Integer Difficulty
future stupendous many apparatus head elderly attraction wise desert shy This post was mass deleted and anonymized with Redact More on reddit.com
🌐 r/leetcode
26
18
November 18, 2022
How to convert a Roman numeral to integer in PHP? - Stack Overflow
Using PHP, I'd like to convert a string containing a Roman number into its integer representation. I need this because I need to make calculations on them. Wikipedia on Roman numerals It would su... More on stackoverflow.com
🌐 stackoverflow.com
[PROJECT] Roman Numeral -> Integer Converter

here is my_code in Python 2. detects incorrect inputs; please suggest any better approach!! Thanks

More on reddit.com
🌐 r/beginnerprojects
16
14
December 20, 2015
Problem 13: Roman to Integer

WOW. I just found out the issue, and it was that I was initializing sum,valOne,valTwo all at once, which caused random numbers to be stored in sum, thus it would sometimes have the wrong answer, although valOne and valTwo were always correct.

More on reddit.com
🌐 r/leetcode
5
3
July 28, 2017
🌐
AlgoMap
algomap.io › problems › roman-to-integer
Roman to Integer - Leetcode Solution
To convert a Roman numeral to an integer, we process the string from left to right. For each character, we look at its value using a fixed mapping. If the character is followed by one of greater value, we interpret the two as a subtractive pair and subtract the current value from the next, adding the result to our running total.
🌐
Medium
medium.com › @AlexanderObregon › solving-the-roman-to-integer-problem-on-leetcode-javascript-solutions-walkthrough-df930969711e
Solving LeetCode's Roman to Integer in JavaScript | Medium
February 11, 2025 - Step 1: Initialize sum to 0 to hold the total of converted integers. Step 2: Iterate through each character s[i] in the string s. Step 3: Retrieve the integer value of the current Roman numeral from the values object.
🌐
Reddit
reddit.com › r/leetcode › roman to integer difficulty
r/leetcode on Reddit: Roman To Integer Difficulty
November 18, 2022 -

I have only started creating an acc in leetcode and the title is the first problem that was given to me, and it was marked as "Easy", and I'd say I'm really frustrated by the fact that I can't even think of the right algorithm to create quickly, considering this was supposed to be easy. I know I'm not that good in programming right now, and I have a lot to learn, but why is this problem supposed to be easy when it is much more complicated than what I think it is? Especially the error handling, I have thought of an algorithm to convert the passed argument to integer, if and only if the input is correct, but I'm having a really bad time thinking of an efficient way to check whether the input is valid roman numeral or not. Is it really that "easy" compared to the medium problems that I have yet to solve?

Top answer
1 of 4
32
future stupendous many apparatus head elderly attraction wise desert shy This post was mass deleted and anonymized with Redact
2 of 4
11
I work at a global tech company that isn't Faang but you would absolutely know. I generally interview for Senior and Principal level positions. I use this problem as my interview question. I love it. It tells me far more about you as a programmer than a leetcode medium or hard. All that I know when you pass a hard is that you studied for the test. I'm not looking for that kind of engineer. When I give this problem it is rare that I get an engineer who has seen it for precisely the comments made on this thread. Then, I get to see somebody work through a straightforward problem with a simple solution. I generally see 4 types of responses: Completely lost, quiet, stumped. Doesn't ask for help or really talk through the problem. These people don't present a solution. Mostly lost. They start trying to solve this like it's a leetcode hard. They often don't work with me as they think they're expected to pull a rabbit out of a hat. Somewhat lost but they see that there's a solution possible. They start work and talk to me about what they're doing. I can offer guidance and often they can solve the problem. Their code may not be beautiful or perfect, but they show me how they think. quick solve. They've seen the problem before or they see the solution quickly. (I've only had this happen once). They talk through the solution, write a beautiful piece of code which is well functionalized, clear, concise, and nearly perfect. 1 and 2 are failures. 4 is a pass, but I'm going to focus on a lot of the other parts of the interview as well as what the other interviewers on the loop say. I'm really looking for 3 because, in reality, that's the job. Most of the time I'm going to ask you to do something you aren't going to know how to do, you won't have studied, and there likely isn't some algorithm you can fall back on having seen before. Our day to day coding tasks rarely require the silly techniques that are the focus of leetcode. Instead they require communication, clarity of thought, and simple solutions to what appear to be complex problems. I try to test for those skills and leave the leetcode hards to our bar raiser.
🌐
CalculatorSoup
calculatorsoup.com › calculators › conversions › roman-numeral-converter.php
Roman Numeral Converter
Convert Roman numerals to numbers or convert numbers to Roman numerals. Find Roman numerals up to 3,999,999 using I, V, X, L, C, D and M.
Find elsewhere
🌐
AlgoMonster
algo.monster › home › 13. roman to integer
13. Roman to Integer - In-Depth Explanation
The implementation uses a hash table combined with a clever traversal pattern to convert Roman numerals to integers.
🌐
Wikipedia
en.wikipedia.org › wiki › Roman_numerals
Roman numerals - Wikipedia
7 hours ago - The use of Roman numerals has continued long after the decline of the Roman Empire. From the 14th century on, Roman numerals began to be replaced by the positional Hindu–Arabic numeral system; however, this process was gradual, and the use of Roman numerals has persisted in some contexts, ...
🌐
LeetCode
leetcode.com › problems › integer-to-roman
Integer to Roman - LeetCode
Can you solve this real interview question? Integer to Roman - Seven different symbols represent Roman numerals with the following values: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 Roman numerals are formed by appending the conversions of decimal place values from highest to lowest.
🌐
Medium
medium.com › @soham-khade › 12-integer-to-roman-leetcode-problem-java-f51fb96b8159
12. Integer to Roman LeetCode Problem (Java) | by Soham | Medium
October 27, 2023 - For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II.
🌐
AlgoMonster
algo.monster › home › 12. integer to roman
12. Integer to Roman - In-Depth Explanation
The conversion follows a fixed greedy table from largest to smallest Roman value, so the solution is direct simulation of the subtraction rules.Open in Flowchart ... First, let's pin down the algorithm using the Flowchart. Here's a step-by-step walkthrough: ... No: The input is a single integer.
🌐
NeetCode
neetcode.io › solutions › 12. integer to roman
LeetCode 12 Integer to Roman Solution & Explanation | NeetCode
When building the Roman numeral string, you must process values from largest to smallest. Starting with smaller values or processing in random order will produce incorrect results. For example, processing 1994 by handling ones before thousands would give an invalid representation. When computing how many times a symbol should appear, some implementations confuse the division and modulo operations. You need integer division to get the count of symbols, then modulo to get the remaining value.
🌐
Medium
medium.com › @kollisnehagowri › title-solving-the-roman-to-integer-problem-from-leetcode-88a1dfe4869b
Solving the Roman to Integer Problem From LeetCode | by Sneha Gowri Kolli | Medium
September 8, 2023 - To convert a Roman numeral into an integer efficiently, we can iterate through the string from left to right and accumulate the values. We need to consider the special cases where a smaller numeral precedes a larger one, indicating subtraction ...
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › leetcode: converting integers to roman numerals
LeetCode: Converting Integers to Roman Numerals | John Kavanagh
January 8, 2024 - Traditional Roman numerals do not have a clean built‑in representation for zero, negative numbers, or very large values without extra notation. For a LeetCode‑style version of the problem, the useful range is usually 1 to 3999. That range decision belongs in the function contract. Either reject values outside it or document that the function expects valid positive integers in that range.
🌐
Medium
medium.com › @fanjum524 › leetcode-problem-no-13-44d63508428c
Leetcode Problem no.13. Roman to Integer | by Farhan | Medium
August 9, 2024 - 6. else: res += roman[s[i]]: If the condition is false, simply add the current character's value to the result. 7. return res: Returns the final calculated integer value.
🌐
Medium
medium.com › @ashwinmani37 › best-solution-for-integer-to-roman-numeral-c-code-ff7ef4f92470
Best Solution for Integer to Roman Numeral C++ Code | by Ashwin Mani | Medium
June 14, 2023 - We would start breaking the number by dividing it with the highest notation provided to us which is M i.e- 1000. If the number is divisible by 1000, we concat ‘M’ in our final string representing 1000 in Roman Numeral and deduct 1000 from the given number.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0000-0099 › 0012.Integer to Roman › README_EN.md
leetcode/solution/0000-0099/0012.Integer to Roman/README_EN.md at main · doocs/leetcode
Converting a decimal place value into a Roman numeral has the following rules: If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
Author: doocs