simple cpp solution
\n\n## Intuition\nWe are given a string s of square brackets. The goal is to find the minimum number of adjacent swaps needed to make the string balanced. A str
\n\n## Intuition\nWe are given a string `s` of square brackets. The goal is to find the minimum number of adjacent swaps needed to make the string balanced. A string is balanced if every opening bracket `[` has a corresponding closing bracket `]` and they are correctly nested.\n\nTo solve this problem, we can focus on identifying the number of unmatched or misplaced brackets, and then calculating the minimum number of swaps required to fix them.\n\n## Approach\n1. **Stack Approach**: Traverse the string and use a stack to track unbalanced brackets. \n   - Push opening brackets `[` onto the stack.\n   - When a closing bracket `]` is encountered, if the stack has an opening bracket `[` on top, pop the stack (as these two brackets form a valid pair). Otherwise, push the closing bracket `]`.\n   - At the end of the traversal, the stack will contain only the unmatched brackets.\n\n2. **Calculating Swaps**:\n   - Once the traversal is complete, the number of unmatched brackets will be twice the number of required swaps, since each swap fixes two brackets (one opening and one closing).\n   - The final answer can be derived by dividing the number of unmatched brackets by 2.\n\n## Complexity\n- **Time Complexity**: \n  - Traversing the string takes linear time, i.e., \\(O(n)\\), where \\(n\\) is the length of the string.\n  - Stack operations (push and pop) take constant time, so the overall time complexity is \\(O(n)\\).\n\n- **Space Complexity**: \n  - In the worst case, we might store half of the characters in the stack if the string is completely unbalanced. Thus, the space complexity is \\(O(n)\\).\n\n## Code\n```cpp\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        // Stack to track unbalanced brackets\n        stack<char> st;\n        \n        // Traverse through the string\n        for(char it : s) {\n            if (!st.empty() && st.top() == \'[\' && it == \']\') {\n                // A valid pair found, pop the stack\n                st.pop();\n            } else {\n                // Push current bracket onto the stack\n                st.push(it);\n            }\n        }\n        \n        // The size of the stack now contains only unmatched brackets\n        int unmatched = st.size();\n        \n        // We need half of the unmatched brackets to swap them\n        // to balance the string (each swap fixes two brackets)\n        return (unmatched / 2);\n    }\n};\n```\n\nThis code efficiently calculates the minimum number of swaps required to balance the given string of square brackets by using a stack to track unbalanced brackets and then calculating the necessary swaps based on the stack\'s contents.
['C++']