(String s)
| 1 | class Sol |
| 2 | { |
| 3 | int countRev (String s) |
| 4 | { |
| 5 | // your code here |
| 6 | if(s.length()%2!=0) return -1; |
| 7 | |
| 8 | int open=0,close=0,rev=0; |
| 9 | |
| 10 | for(int i=0;i<s.length();i++) |
| 11 | { |
| 12 | if(s.charAt(i)=='{') open++; |
| 13 | else |
| 14 | { |
| 15 | if(open==0) close++; |
| 16 | else open--; |
| 17 | } |
| 18 | } |
| 19 | rev=(int)(Math.ceil(open/2.0) + Math.ceil(close/2.0)); |
| 20 | return rev; |
| 21 | } |
| 22 | } |