Number of swaps to make the string balanced - Nice Explanation - 🌟Beats 100%🌟
Intuition\n1. Calculating number of unbalanced ] and then using that we may calculate the number of swaps by dividing that unbalanced by 2 leaved remainder if i
# Intuition\n1. Calculating number of unbalanced ] and then using that we may calculate the number of swaps by dividing that unbalanced by 2 leaved remainder if it\'s not even.\n\n1. Hence we also need to add remainder to that term since remainder will also need to be swapped and it can be either 0 (even) or 1(odd).\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n**Initialization**:\n   1. Initialize stkPointer which is needed to keep track of number of characters that may enter the stack(In our case: [).\n   1. Initialize unbalanced to 0, which keep track of number of ] that doesn\'t have pair of [.\n             \n    int stkPointer=0;\n    int unbalanced=0;\n\n**Iterate through the string**:\n   1. Iterate through each character in the string by encoding the string and getting each byte from the string.\n   1. getBytes() actually get encoding format as parameter , if not specified it\'ll take default format.\n   1. This is more efficient than converting the string to character array and getting character by character. \n   \n     for(byte b:s.getBytes())\n\n**Calculating number of unbalanced ]**:\n    If the byte or character that is [ , it should go into the stack.\n    Hence increment the stkPointer.\n   \n     if(b==\'[\')\n       stkPointer++;\n\n   If the character is ], then we should check if the stack contains element or not.\n   This can be done by checking if stkPointer is greater than 0, \n       If so we\'ll pop the corresping [, hence decrement the stkPointer by 1\n      Otherwise, increment the unbalanced since ] doesn\'t have equivalent [.\n           \n    else{ \n\n    if(stkPointer>0)\n             stkPointer--;\n    else\n        unbalanced++;\n    }\n\n**Returning number of swaps**:\n   1. Number of swaps needed can be calculated by dividing the unbalanced by 2 and if it is odd , we may also add one by taking modulo with 2 , the unbalanced.\n\n    return unbalanced/2 + unbalanced%2 ;\n\n\n   \n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- *Time complexity*: **O(n)**.\n   n -> the number of characters in the string.\n\n**Iteration through the string**: O(n).\n   1. The getBytes() method converts the string s into a byte array, which takes **O(n) time**, where n is the length of the string s.\n   1. The for loop iterates over each byte in this array, and for each character, we perform **constant-time operations** (checking whether the byte is [ or ], and adjusting the stkPointer and unbalanced counters).\n\n**Operations inside loop**: O(1).\n   1. Inside the loop, you are performing basic comparison and increment/decrement operations, which are **O(1) for each character**.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- *Space complexity*: **O(n)**.\n    n -> number of characters in the string.\n\n**Stack Pointer and unbalanced Counter**: O(1).\n   1. Both stkPointer and unbalanced are integers, so they take **constant space O(1)**.\n\n**Bytes array from getBytes()**: O(n).\n   1. The **getBytes()** method creates a **byte array** that stores a byte for each character in the string s. \n   1. The size of this array is proportional to the **length of the string** s, so it requires **O(n) space**.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int minSwaps(String s) {\n        // we are using stack\n        //n/2 open and n/2  close brackets\n        // AB where A and B is balanced string -> A = [] B=[] So [][]\n        // [C] where C is a balanced string -> C=[] -> [[]]\n        // May swap the brackets at any 2 indices any number of times\n        // Minimum swap to make the s balanced.\n\n        int stkPointer=0;\n        int unbalanced=0;\n        for(byte b:s.getBytes())\n        {\n            \n            if(b==\'[\')\n              stkPointer++;\n            else\n            {\n                if(stkPointer>0)\n                  stkPointer--;\n                else\n                  unbalanced++;\n            }\n        }\n        return unbalanced/2 + unbalanced%2 ;\n\n\n    }\n}\n```
['String', 'Stack', 'Java']