-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCountReversals.js
51 lines (46 loc) · 1.19 KB
/
CountReversals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @author Anirudh Sharma
*
* Given a string S consisting only of opening and closing curly brackets '{' and '}'
* find out the minimum number of reversals required to make a balanced expression.
*
* If it cannot be balanced, then print -1.
*/
const count = (s) => {
// Length of the string
const n = s.length;
// If the length is odd then we cannot make
// balanced expression
if (n % 2 === 1) {
return -1;
}
// Count of left and right braces
let leftBraceCount = 0;
let rightBraceCount = 0;
// Loop through the string
for (let i = 0; i < n; i++) {
// Current character
let c = s.charAt(i);
if (c === '{') {
leftBraceCount++;
} else {
if (leftBraceCount === 0) {
rightBraceCount++;
} else {
leftBraceCount--;
}
}
}
return (Math.ceil(leftBraceCount / 2.0) + Math.ceil(rightBraceCount / 2.0));
};
const main = () => {
let s = "}{{}}{{{";
console.log(count(s));
s = "{{}}}}";
console.log(count(s));
s = "{{}{{{}{{}}{{";
console.log(count(s));
s = "{{{{}}}}";
console.log(count(s));
};
main();