educative.io

Educative

It appears there is a more compact solution than the one given in the problem

public static int romanToInt(String s) {
	
	if(s == null || s.isEmpty()) {
		return 0;
	}

	int res = 0;
	int prev = 0;
	int curr = 0;
	for(int i = s.length() - 1; i >= 0; i--) {
		curr = values.get(s.charAt(i));
		if(curr >= prev) {
			res += curr;
		}
		else {
			res -= curr;
		}
		prev = curr;
	}
	
	return res;
}

Type your question above this line.

Course: https://www.educative.io/collection/5642554087309312/5679846214598656
Lesson: https://www.educative.io/collection/page/5642554087309312/5679846214598656/5711987161432064

Hi @Peter_Litvak,

Yes, your solution seems valid for the provided code problem.
Thank you.