Reputation: 6960
Given the following mapping:
0->J, 1->I, 2->H, 3->G , 4->F, 5->E, 6->D, 7->C, 8->B, 9->A
And also given a numeric string, the expected output would be the latter mapped accordingly.
E.g. If the input is "2018", the expected ouput is "HJIB".
My question is: is there a procedure more efficient than the straightforward approach of iterating each character of the input string and map it? More specifically, I would like to know if I can do better than something like:
String output="";
for(int i=0; i<input.length(); i++) {
output=output+map(input.charAt(0));
}
Upvotes: 0
Views: 88
Reputation: 43748
Savings are possible in the part you have not shown (I assume map
is a function).
Initialize an array with the mapping, i.e. m[0] = 'J', m[1] = 'I', ...
Then do m[input.charAt(i)]
instead of map(input.charAt(i))
.
And of course use a StringBuilder
instead of the string concatenation (assuming this is Java).
Upvotes: 1