✔ Intuitive Greedy Solution || C++
Whenever we encounter close Bracket > Open Bracket , we optimally swap close with the \nclosest open bracket from the end of the string . \nThis approach is qui
Whenever we encounter close Bracket > Open Bracket , we optimally swap close with the \nclosest open bracket from the end of the string . \nThis approach is quite intuitive as it is the first thing that comes to mind .\nWe just have to maintain 2 pointers so that we know how much charecters from end we have already checked \nand neednot visit again. Thats why we are running the loop **while(i<=j)**\n\n```\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int open = 0 , close = 0;\n        int swaps = 0;\n        int i = 0 , j = s.size()-1 ;\n        \n        while(i <= j){\n            if(s[i] == \'[\') open++;\n            else if( s[i] == \']\') close++;\n            \n            if(close > open)\n            {\n                while(s[j] == \']\') j--;\n                swap(s[i] , s[j]) ;\n                close-- , open++ , swaps++ , j-- ;\n            }\n            \n            i++;\n        }\n        \n        return swaps ;\n    }\n};
[]