Convert a number string to the largest possible value by changing at most one digit according to the allowed currency digit mapping.
You are given a number written in a special currency system where each digit can be converted to another digit according to a fixed mapping. Your task is to make the resulting number as large as possible by applying the conversion rules to at most one digit, or to all digits if that is required by the exact rule set of the problem.
Read the digits of the number, apply the allowed transformation carefully, and output the resulting number in the same order. The key is to follow the digit-mapping rule exactly and choose the best position to improve the value when multiple choices are possible.
Input Format
Input
- The first line contains an integer — the length of the number.
- The second line contains a string of digits.
- The next lines describe the digit transformation rules or the relevant mapping for this currency system.
Notes
- This is an implementation-style problem.
- Treat the number as a string; leading zeros, if any, should be preserved unless the rules explicitly change them.
Output Format
Output
- Print the transformed number after applying the best valid conversion.
If there are multiple valid answers, print any one that satisfies the rules.
Constraints
- Digits are decimal characters.
- The solution should run in linear time or close to it.
Example 1
Input
5 12345
Output
52345
Explanation
If the rules allow changing one digit to a larger one, the best choice is to improve the earliest possible digit, since that has the biggest impact on the final value.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.