Connection Status:
Competition Arena > SwappingDigits
SRM 583 · 2012-12-13 · by blue.boy · Brute Force
Class Name: SwappingDigits
Return Type: String
Method Name: minNumber
Arg Types: (string)
Problem Statement

Problem Statement

Given is a String num. This String contains the digits of a (possibly large) positive integer. For example, num="1147" represents the integer 1147. The String num will not have any leading zeros.

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 String must again describe a valid positive integer, i.e., it must not have any leading zeros.

Find and return the String that represents the smallest possible integer that can be obtained.

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'.
Examples
0)
"596"
Returns: "569"

You can swap num[1] and num[2] to get the minimum integer.

1)
"93561"
Returns: "13569"
2)
"5491727514"
Returns: "1491727554"
3)
"78326141480732117541253865198971"
Returns: "18326141480732117541253865198977"
4)
"539613437590740162597117122494746412"
Returns: "139613437590740162597117122494746452"
35)
"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.

Coding Area

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

Submitting as anonymous