MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

minimumAdjacentSwapsForBalanceBrackets.java:2–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//User function Template for Java
2class Solution{
3 static int minimumNumberOfSwaps(String S){
4 // code here
5 int open=0;
6 int close=0;
7 int swap=0;
8 int imbalance=0;
9 for(int i=0;i<S.length();i++)
10 {
11 if(S.charAt(i)=='[')
12 {
13 open++;
14 if(imbalance>0)
15 {
16 swap+=imbalance;
17 imbalance--;
18 }
19 }
20 else
21 {
22 close++;
23 imbalance=close-open;
24 }
25 }
26 return swap;
27 }
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected