# More Generalizable Solution. Can be extended to any Roman to integer conversion.

• This solution includes generalizability. Even if the number to be converted is > 3999, it works without making extra effort for finding cases.

``````bool isMul(int n) {
return (n + 1) % 5 == 0;
}
int getMult(const int & n) {
if(n / 1000 > 0) return 1000;
if(n / 100 > 0) return 100;
if(n / 10 > 0) return 10;
return 1;
}
string subProb(int &num) {
map<int,string> val;
val[1] = "I";
val[5] = "V";
val[10] = "X";
val[50] = "L";
val[100] = "C";
val[500] = "D";
val[1000] = "M";
int multiple = getMult(num);
string ans = "";
int value = num / multiple;
if(value == 4) ans += (val[multiple] + val[5 * multiple]), value -=4;
else if(value == 9) ans += (val[multiple] + val[10 * multiple]), value -=9;
else if(value >= 5) {
ans += val[5 * multiple];
value -= 5;
}
for(int i = 0; i < value; i ++) ans += val[multiple];
num %= multiple;
return ans;
}
string intToRoman(int num) {
string ans = "";
while(num > 0) ans += subProb(num);
return ans;

}
``````

Hope you like the solution

• Brilliant solution :)

• @bahetysudhanshu I think this is the best solution for basic number of Roman numerals is only 7.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.