Maximum Swap LeetCode Solution

Last updated on October 5th, 2024 at 04:24 pm

Here, We see Maximum Swap LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Array, Math

Companies

Facebook

Level of Question

Medium

Maximum Swap LeetCode Solution

Maximum Swap LeetCode Solution

Problem Statement

You are given an integer num. You can swap two digits at most once to get the maximum valued number. Return the maximum valued number you can get.

Example 1:
Input: num = 2736
Output: 7236
Explanation: Swap the number 2 and the number 7.

Example 2:
Input: num = 9973
Output: 9973
Explanation: No swap.

1. Maximum Swap Leetcode Solution C++

class Solution {
public:
    int maximumSwap(int num) {
        string s = to_string(num);
        string s1 = s;
        for(int i=0;i<s.length();i++)
        {
            int temp = getmax(s,i);
            swap(s[i],s[temp]);
            if(s1!=s)
                break;
        }
        int number = stoi(s);
        return number;       
    }
        static int getmax(string s, int i){
        int max=i;
        for(int j=i;j<s.length();j++)
        {
            if(s[max]<=s[j])
                max=j;
        }
        return max;
    }
};

2. Maximum Swap Leetcode Solution Java

class Solution {
    public int maximumSwap(int num) {
        if(num < 10) return num;
        char[] arr = String.valueOf(num).toCharArray();
        int[] rightIndex = new int[10];
        for(int i=0; i<arr.length; i++){
            rightIndex[arr[i] - '0'] = i;
        }
        for(int i=0; i<arr.length; i++){
            for(int j=9; j>arr[i] - '0'; j--){
                if(rightIndex[j] > i){
                    char temp = arr[i];
                    arr[i] = arr[rightIndex[j]];
                    arr[rightIndex[j]] = temp;
                    return Integer.valueOf(new String(arr));
                }
            }
        }
        return num;
    }
}

3. Maximum Swap Leetcode Solution JavaScript

var maximumSwap = function(num) {
    const digits = num.toString().split("");
    let max = -1, maxIdx = -1, leftIdx = -1, rightIdx = -1;
    for(let i = digits.length - 1; i >= 0; i--) {
        const digit = parseInt(digits[i]);
        if(digit > max) max = digit, maxIdx = i;
        else if(digit < max) leftIdx = i, rightIdx = maxIdx;
    }
    if(leftIdx === -1) return num;
    [digits[leftIdx],digits[rightIdx]] = [digits[rightIdx],digits[leftIdx]];
    return parseInt(digits.join(""));
};

4. Maximum Swap Leetcode Solution Python

class Solution(object):
    def maximumSwap(self, num):
        s = list(str(num))
        n = len(s)
        for i in range(n-1):
            if s[i] < s[i+1]: break
        else: return num
        max_idx, max_val = i+1, s[i+1]
        for j in range(i+1, n):
            if max_val <= s[j]: max_idx, max_val = j, s[j]
        left_idx = i
        for j in range(i, -1, -1):    
            if s[j] < max_val: left_idx = j
        s[max_idx], s[left_idx] = s[left_idx], s[max_idx]
        return int(''.join(s)) 
Scroll to Top