🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
\n\n---\n\n\n# Intuition\nThe problem revolves around balancing the square brackets [ and ]. If there are more closing brackets than opening ones at any point, 
![image.png](https://assets.leetcode.com/users/images/55b5dc5a-0603-4383-a04e-01ea9f601e17_1728404425.4553542.png)\n\n---\n\n\n# Intuition\nThe problem revolves around balancing the square brackets `[` and `]`. If there are more closing brackets than opening ones at any point, it leads to an imbalance. To fix this imbalance with the fewest swaps, we can count how many brackets are mismatched.\n\n---\n\n# Approach\n1. **Use a stack for tracking mismatches**: We traverse the string and push opening brackets `[` onto the stack. For closing brackets `]`, if the stack is not empty and the last element is an opening bracket `[`, we pop the stack (indicating a valid pair). Otherwise, we add the closing bracket `]` to the stack, indicating an imbalance.\n2. **Calculate the number of swaps**: At the end, the stack will contain the number of unmatched brackets. Half of these are unmatched closing brackets and half are unmatched opening brackets. To fix the imbalance, we need `(b+1)//2` swaps, where `b` is half the size of the remaining stack.\n\n---\n\n# Complexity\n- **Time Complexity**: \\(O(n)\\), where \\(n\\) is the length of the string, since each character is processed once.\n- **Space Complexity**: \\(O(n)\\), because we use a stack to store mismatched brackets.\n\n---\n\n```dart []\nclass Solution {\n  int minSwaps(String s) {\n    List<String> stack = [];\n    for (String c in s.split("")) {\n      if (c == "[") {\n        stack.add(c);\n      } else {\n        if (stack.isEmpty || stack.last == "]") {\n          stack.add(c);\n        } else {\n          stack.removeLast();\n        }\n      }\n    }\n    int b = stack.length ~/ 2;\n    return (b + 1) ~/ 2;\n  }\n}\n```\n\n```c []\n#include <stdio.h>\n#include <string.h>\n\nint minSwaps(char* s) {\n    int stack = 0, imbalance = 0;\n    for (int i = 0; i < strlen(s); i++) {\n        if (s[i] == \'[\') {\n            stack++;\n        } else {\n            if (stack > 0) {\n                stack--;\n            } else {\n                imbalance++;\n            }\n        }\n    }\n    return (imbalance + 1) / 2;\n}\n\nint main() {\n    char s[] = "[]][][";\n    printf("Minimum swaps: %d\\n", minSwaps(s));\n    return 0;\n}\n```\n\n```cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int stack = 0, imbalance = 0;\n        for (char c : s) {\n            if (c == \'[\') {\n                stack++;\n            } else {\n                if (stack > 0) {\n                    stack--;\n                } else {\n                    imbalance++;\n                }\n            }\n        }\n        return (imbalance + 1) / 2;\n    }\n};\n```\n\n```java []\nclass Solution {\n    public int minSwaps(String s) {\n        int stack = 0, imbalance = 0;\n        for (char c : s.toCharArray()) {\n            if (c == \'[\') {\n                stack++;\n            } else {\n                if (stack > 0) {\n                    stack--;\n                } else {\n                    imbalance++;\n                }\n            }\n        }\n        return (imbalance + 1) / 2;\n    }\n}\n```\n\n```javascript []\nvar minSwaps = function(s) {\n    let stack = 0, imbalance = 0;\n    for (let c of s) {\n        if (c === \'[\') {\n            stack++;\n        } else {\n            if (stack > 0) {\n                stack--;\n            } else {\n                imbalance++;\n            }\n        }\n    }\n    return (imbalance + 1) / 2;\n};\n```\n\n```python []\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        stack = 0\n        imbalance = 0\n        for c in s:\n            if c == \'[\':\n                stack += 1\n            else:\n                if stack > 0:\n                    stack -= 1\n                else:\n                    imbalance += 1\n        return (imbalance + 1) // 2\n```\n\n```go []\nfunc minSwaps(s string) int {\n    stack, imbalance := 0, 0\n    for _, c := range s {\n        if c == \'[\' {\n            stack++\n        } else {\n            if stack > 0 {\n                stack--\n            } else {\n                imbalance++\n            }\n        }\n    }\n    return (imbalance + 1) / 2\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style=\'width:250px\'}
['C', 'C++', 'Go', 'Python3', 'JavaScript', 'Dart']