<p>You are given an integer <code>num</code>. You can swap two digits at most once to get the maximum valued number.</p>

<p>Return <em>the maximum valued number you can get</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = 2736
<strong>Output:</strong> 7236
<strong>Explanation:</strong> Swap the number 2 and the number 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = 9973
<strong>Output:</strong> 9973
<strong>Explanation:</strong> No swap.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= num &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution {
public:
    int maximumSwap(int num) {
        stack<int> st;
        
        string str = to_string(num);
        int n = str.size();
        
        for(int i=n-1;i>=0;i--){
            if(st.empty() || str[st.top()]<=str[i]){
                st.push(i);
                // cout<<i<<" ";
            }
            
        }
        int id = 0;
        while(!st.empty() && str[st.top()]==str[id]){          
            // cout<<st.top()<<" "<<id<<endl;
            st.pop();
            id++;
        }
        
        
        while(!st.empty()){
            int temp = st.top();
            st.pop();
            if(!st.empty() && str[temp]!=str[st.top()]) {
                st.push(temp); 
                break;   
            }
            else if(st.empty()) {
                st.push(temp); 
                break;
            }
        }
        
        if(st.empty()) return num;
        
        // cout<<str<<" "<<st.top()<<endl;
        
        swap(str[st.top()], str[id]);
        // cout<<str<<endl;
        int number = stoi(str);
        return number;
    }
};
7800