Given two string A and B which represents two integers, the task is to print the maximized value of A after replacing 0 or more digits of A with any digit of B.
Note: A digit in B can only be used once.
Input: A = “1234”, B = “4321”
1 can be replaced with 4 and 2 can be replaced with 3.
Input: A = “1002”, B = “100”
The first 0 can be replaced with a 1.
Approach: Since the value of A has to maximized, any digit will be replaced by only digits of greater value. The digits on the left have more significance in contributing to the value, so they should be replaced with as large values as possible. Sort B and iterate from left to right in A and try replacing the current digit with the maximum of available options if possible.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Program to find last two digits of 2^n
- First N terms whose sum of digits is a multiple of 10
- Absolute difference between the first X and last X Digits of N
- Pairs whose concatenation contain all digits
- Print numbers with digits 0 and 1 only such that their sum is N
- Check if given string contains all the digits
- Check if the sum of digits of N is palindrome
- Smallest odd digits number not less than N
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Count numbers less than N containing digits from the given set : Digit DP
- Check if a number has digits in the given Order
- Count changes in Led Lights to display digits one by one
- Maximum possible time that can be formed from four digits
- Find the last two digits of Factorial of a given Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.