C++ || EASY TO UNDERSTAND || simple solution || brute force
Brute force\n\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int n=s.size(),ans=0;;\n        int opening=0,closing=0;\n          int j=n-1;\n
**Brute force**\n```\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int n=s.size(),ans=0;;\n        int opening=0,closing=0;\n          int j=n-1;\n       for(int i=0;i<n;i++)\n       {\n           if(s[i]==\'[\')\n           {\n               opening++;\n           }\n           else\n           {\n               closing++;\n           }\n           if(closing>opening)\n           {\n               while(j>=0&&s[j]!=\'[\')\n               {\n                   j--;\n               }\n               swap(s[i],s[j]);\n               closing--;\n               opening++;\n               ans++;\n           }\n       }\n        return ans;\n    }\n};\n```\n**Likewise**\n```\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int n=s.size(),ans=0;;\n        int diff=0;\n          int j=n-1;\n       for(int i=0;i<n;i++)\n       {\n           if(s[i]==\'[\')\n           {\n               diff++;\n           }\n           else\n           {\n               diff--;\n           }\n           if(diff<0)\n           {\n               while(j>=0&&s[j]!=\'[\')\n               {\n                   j--;\n               }\n               swap(s[i],s[j]);\n               diff=1;\n               ans++;\n           }\n       }\n        return ans;\n    }\n};\n```\n\n**Please upvote to motivate me in my quest of documenting all leetcode solutions(to help the community). HAPPY CODING:)\nAny suggestions and improvements are always welcome**
[]