[Python] - Simple Greedy Solution | Time=O(N)  Space=O(1)
Intuition\nThe correct solution will be a valid sequence of brackets. \n\ni.e./ the # of open brackets are always >= # closing brackets at any point in the stri
# Intuition\nThe correct solution will be a valid sequence of brackets. \n\ni.e./ the `# of open brackets` are always >= `# closing brackets` at any point in the string.\n\nWe must fix the string.\n\n# Approach\n\nTo fix the string we simply need to ensure that every close bracket has an open bracket. We simply change invalid close brackets to open brackets. \n\n**As the problem states there is an equal number of closing brackets and open brackets, we can assume that we will optimally swap a bad closed bracket with an open bracket later in the string. Note: we don\'t actually have to do the swaps, merely counting the required swaps is good enough!\n\nE.g./\n```\ns = "]]][][[["\n\nsteps:\n"]" -- bad close, fixed = 1\n"[]" -- ok\n"[]]" -- bad close, fixed = 2\n"[][[" -- ok\n"[][[]" -- good close, ok\n"[][[][" -- ok\n"[][[][[" -- ok\n"[][[][[[" -- ok\n```\n\nAfter fixing 2 bad closing brackets it appears that we have 4 open brackets remaining. To simulate a swap, we can replace 2 open brackets anywhere in the string such that it becomes a "perfectly balanced string". A few possible outcomes:\n\n```\n"[][[][]]"\n"[][[]][]"\n```\n\nAs the question doesn\'t require the string, we could just return the number of fixes applied.\n\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        fixes_applied = 0\n        current_open = 0\n\n        for c in s:\n            if c == \'[\':            # Open is always considered OK\n                current_open += 1\n\n            elif current_open > 0:  # Valid Closings are OK\n                current_open -= 1\n                \n            else:                   # Invalid Closings Require a Fix\n                fixes_applied += 1\n                current_open += 1\n\n        return fixes_applied\n\n```
['Python', 'Python3']