I am writing code to convert Roman Numbers to Decimal Numbers.
I was wondering if this line could be optimised?
String s = "MCMXCIV";
String code = s.replaceAll("I", "a")
.replaceAll("V", "b")
.replaceAll("X", "c")
.replaceAll("L", "d")
.replaceAll("C", "e")
.replaceAll("D", "f")
.replaceAll("M", "g");
Expected value of code
: gegceab
EDIT 1: Can the same code can be written in a shorter way?
EDIT 2:
Two conditions I want
- Avoid expensive string concatenation
- replace each character with corresponding code in
O(1)