gfhd760
BAN USERSome facts must be given about roman numerals:
-There are 7 symbols I V X L C D M.
-Each symbol has the following respective integer values 1 5 10 50 100 500 1000.
-The symbols in a roman string are summed in a special way such that the string matches the value known.
Using the information above we can work out a few examples that show us how the value of a roman string is calculated.
Ex 1. LXXXVI =86 this can be expanded into the sum 1 + 5 +10 +10 + 10 + 50.
Ex 2. MDCC =1700 =100 + 100 + 500 + 1000.
Ex 3. IV =4 = 5 + (-1).
Strategy:
Starting from the last symbol of the roman string we add to the total.
Then we go to the next symbol to the left.
If the value of this symbol is smaller than the last symbol we looked at let us subtract it otherwise add it to the total.
Continue onto process symbols like this until we have processed the first symbol of the roman string.
Java Snippet:
int romanNumber(String roman){
int valueOfLastChar = 0, valueOfCurrentChar =0;
int total = 0;
for(int i= roman.length()-1; 0<=i; i--){
switch(roman.charAt(i)){
case 'I': valueOfCurrentChar = 1; break;
case 'V': valueOfCurrentChar = 5; break;
case 'X': valueOfCurrentChar = 10; break;
case 'L': valueOfCurrentChar = 50; break;
case 'C': valueOfCurrentChar = 100; break;
case 'D': valueOfCurrentChar = 500; break;
case 'M': valueOfCurrentChar = 1000; break;
}
total += (valueOfCurrentChar < valueOfLastChar) ? -1*valueOfCurrentChar : valueOfCurrentChar;
valueOfLastChar = valueOfCurrentChar;
}
return total;
}
- gfhd760 May 01, 2016
"flips of characters" is ambiguous. The implementation will vary depending on how the interviewer defines a "flip of characters".
- gfhd760 December 14, 2017For example, given a = "ate" and b ="eat"
Is turning a.charAt(0) from an 'a' to an 'e' considered a flip?
If so then this results in 'a' being flipped, 't' being flipped and 'e' being flipped which would result in 3 flips.
Or alternatively is a flip a sort of a swap between the 'e' and 'a' in "ate" so that way e is put in place and string a becomes "eta"
Which means 'e' and 'a' are flipped and then 't' and 'a' are flipped resulting in 2 flips or swaps?
So did you ask the interviewer what they meant? If so can you please give some examples or more explanation on what this means in your question.