Solving RomanToInteger leetCode problem

Dear all,
I am trying to solve the problem of RomanToInteger on my own without looking for online solution, so, I’ve come up with this code using Regex, but when I tried to submit, the console gave me error when the input ( s=III ) as the screenshot shows below.
So, can anyone help me to adjust my Regex formula to avoid this error ?, Thanks in advance.

/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
    
    
    let regex1 = /IV|IX|XL|XC|CD|CM/g;
let result1 = s.match(regex1);
console.log(result1);

let regex2 = /[^IV]|[^IX]|[^XL]|[^XC]|[^CD]|[^CM]/;
// let regex2 = /[^IV]|[^IX]|[^XL]|[^XC]|[^CD]|[^CM]/g;
let result2 = s.match(regex2);
console.log(result2);

let total = [];
if( result1 !== null && result2 !== null){
    total = result2.concat(result1);
}else if(result1 === null){
    total = result2;
}else if(result2 === null){
    total = result1;
}

console.log(total);


let temp;
let count = 0;

const jsonnn = new Map([

["I", 1],
["V", 5],
["X", 10],
["L", 50],
["C", 100],
["D", 500],
["M", 1000],
["IV", 4],
["IX", 9],
["XL", 40],
["XC", 90],
["CD", 400],
["CM", 900]

]);

for(let i=0; i<total.length; i++){
    if(jsonnn.has(total[i])){
    temp = jsonnn.get(total[i]);
    // count = temp;
    }
    count += temp;
}

console.log(count);
return count;

    
};

Explain in your own words what you think this regex is attempting to match.

Hi there, you may want to use a tool like this online regex tool to help you work on debugging your regex.

https://regex101.com/

1 Like

many thanks, great website …

1 Like

I think I am trying to negate the values inside the square brackets , because these are special values, so, I am telling (if you ever find these special values, put them in total as a separate entity) . For example, when I am trying to run this (romanToInt(“MCMXCIV”);), the result = 1994 which is correct. My problem appears when the roman number repeats like (III), it will appear as a single (I). So, my question is how can I edit my regex to get it more than once. By the way, when I used the commented line with (/ /g) , the (III) became correct, but ( MCMXCIV) became wrong :frowning: .
so, is there a combination between the two regex I missed ?

Without having to think to long about, I think one way (using regex) would just to explicitly look for those letters repeat and then use the length of the string matched to know how many to use in the calculation. Then, when they are found, split them up.

I am not guaranteeing this will work because you will still have to modify your other regexes and logic to possibly pull this off.

The other obvious option is to really think about the algorithm needed to solve the problem and then write the code that accomplishes the task. Trying to force a specific syntax of code without mapping out the algorithm is a sure way to get frustrated.

Typically when you know a valid algorithm for a problem, you will recognize the best approach to writing the code. It would seem you are kind of hoping regex will save the day without understanding what you are trying to accomplish with the regex.

ok, let me ask you something, how am I suppose to know which algorithm is valid for this kind of problem?.
I am trying to solve all the problems by myself without looking for online solution, may be my solution is primitive, but when I searched I found that there is a solution by using Regex, that’s why I asked for modifying my Regex or find my mistake.

To know if the algorithm will work you probably should try to come up with it first so that a human can use it to solve the problem.

For eg. When a child learns to add at first they may count up from 1 on one hand and then count up again to the next number and see how many fingers they have used and that is the total sum. This is a type of algorithm and it works for small sums of 10 or less. But breaks for much larger sums (not enough fingers). Therefore just try out your algorithm for different values to confirm it works and if not you must either adjust it (use a third hand? :smile:) or come up with something new that can deal with the different values.

I know, I am just asking about the famous algorithm (selection sort, quick sort, binary search, BFS, DFS, … ) . the question is which type of algorithm fits best for this type of problem?. I know there are many ways to solve one problem, but always there is the best practice. I saw a video of solving RomanToInteger on youtube, and he solved it in a good way, but how can I know if his solution is the best practice or not?
Do you understand me?
For example ( sum problem best solved by “” algorithm, chunky monkey problem best solved by “” algorithm , and so on …

I am fairly sure that the suggestion from Randell earlier was not about “best practice” at all but rather to say that there may be another method/way that you already understand that you can code to. And the purpose of these exercises is to practice your coding so if you are looking up the algorithm for something you as a -human- already know how to do, then that is not the best use of your practice time (part of practicing to code is thinking logically and breaking down tasks for a computer to consume).

If on the other hand you don’t know how to solve a problem yourself (with some paper and pen), then you don’t have to reinvent the wheel and can see how someone with the required skill set solved it first (not in code but in a theoretical way) then write code to match that. I personally did that for any harder physics/chemistry problem for eg as there I am personally weak in the those subjects and need help to know how to solve problems for them. But once I know the method used, I can code it.

Sometimes programs need to meet certain efficiency standards. In that case you may need to use data structures or algorithms that are considered “best practice” to solve them. And those are researched online and if you are on the cutting edge of something, then perhaps you are the one writing a journal article showing that your method is best. But even then, because this is not a formal engineering practice, what is best is subjective to the industry and to the company and even to the person. (If you work for Microsoft they may consider certain things to be best vs Google who consider others to be).

1 Like