{"id":"fc43d732-3d08-4251-bb26-3658164adef8","name":"Maximum Swap","description":"1. You are given a number in form of String. \r\n2. You can swap two digits at most once to get the maximum valued number in that string.\r\n3. Return the maximum valued number you can get in form of string.\r\n","inputFormat":"num = \"2736\"\r\n","outputFormat":"res = \"7236\"\r\nExplanation : swap 2 and 7 to get maximum values","constraints":"0 &lt; num.lenght() &lt; 10^5\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nstring maximumSwap(string &arr) {\n // write your code here\n}\n\nint main() {\n string str;\n cin>>str;\n string ans = maximumSwap(str);\n cout<<ans;\n}\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~User Section~~~~~~~~~~~\r\n public static String maximumSwap(String num) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~Input Management~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String str = scn.nextLine();\r\n String ans = maximumSwap(str);\r\n System.out.println(ans);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2736","sampleOutput":"7236","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"0f370e39-bf9e-4037-92e4-9d6ff130297c","name":"Maximum Swap","slug":"maximum-swap","type":1}],"next":{"id":"1ed1e008-6412-4850-b4f9-bcca121a9d04","name":"Maximum Swap","type":3,"slug":"maximum-swap"},"prev":{"id":"61b77443-70a9-449d-a80e-be36b3453116","name":"4 Sum - Target Sum With Unique Quadruple MCQ","type":0,"slug":"4-sum-target-sum-with-unique-quadruple-mcq"}}}

Maximum Swap

1. You are given a number in form of String. 2. You can swap two digits at most once to get the maximum valued number in that string. 3. Return the maximum valued number you can get in form of string.

{"id":"fc43d732-3d08-4251-bb26-3658164adef8","name":"Maximum Swap","description":"1. You are given a number in form of String. \r\n2. You can swap two digits at most once to get the maximum valued number in that string.\r\n3. Return the maximum valued number you can get in form of string.\r\n","inputFormat":"num = \"2736\"\r\n","outputFormat":"res = \"7236\"\r\nExplanation : swap 2 and 7 to get maximum values","constraints":"0 &lt; num.lenght() &lt; 10^5\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nstring maximumSwap(string &arr) {\n // write your code here\n}\n\nint main() {\n string str;\n cin>>str;\n string ans = maximumSwap(str);\n cout<<ans;\n}\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~User Section~~~~~~~~~~~\r\n public static String maximumSwap(String num) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~Input Management~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String str = scn.nextLine();\r\n String ans = maximumSwap(str);\r\n System.out.println(ans);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2736","sampleOutput":"7236","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"0f370e39-bf9e-4037-92e4-9d6ff130297c","name":"Maximum Swap","slug":"maximum-swap","type":1}],"next":{"id":"1ed1e008-6412-4850-b4f9-bcca121a9d04","name":"Maximum Swap","type":3,"slug":"maximum-swap"},"prev":{"id":"61b77443-70a9-449d-a80e-be36b3453116","name":"4 Sum - Target Sum With Unique Quadruple MCQ","type":0,"slug":"4-sum-target-sum-with-unique-quadruple-mcq"}}}
plane

Editor


Loading...

Maximum Swap

easy

1. You are given a number in form of String. 2. You can swap two digits at most once to get the maximum valued number in that string. 3. Return the maximum valued number you can get in form of string.

Constraints

0 < num.lenght() < 10^5

Format

Input

num = "2736"

Output

res = "7236" Explanation : swap 2 and 7 to get maximum values

Example

Sample Input

2736

Sample Output

7236

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode