SwappingDigits
SRM 583 · 2012-12-13 · by blue.boy
Problem Statement
Given is a
You are allowed to swap one pair of digits in the given number. In other words, you may choose a pair of distinct indices i and j, and swap the characters num[i] and num[j]. Note that you may also leave the original number unchanged. The new
Find and return the
Constraints
- The length of num will be between 2 and 50, inclusive.
- Each character of num will be between '0' and '9', inclusive.
- The first character of num will not be '0'.
"596" Returns: "569"
You can swap num[1] and num[2] to get the minimum integer.
"93561" Returns: "13569"
"5491727514" Returns: "1491727554"
"78326141480732117541253865198971" Returns: "18326141480732117541253865198977"
"539613437590740162597117122494746412" Returns: "139613437590740162597117122494746452"
"10234" Returns: "10234"
You can leave the original String num unchanged. Note that the result must not contain leading zeros.
Submissions are judged against all 141 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SwappingDigits with a public method string minNumber(string num) · 141 test cases · 2 s / 256 MB per case