Standard Bar Strings Hard · Hard O(n) · O(1)
A telemetry parser rejects any packet whose bracket pattern is unbalanced — every opener needs a later closer, and no closer may arrive unpaired. Patching firmware per packet is expensive, so the gateway instead counts the fewest single brackets to splice in anywhere to legitimize the stream.
Input: A string s containing only '(' and ')'.
Output: Return the minimum number of bracket insertions needed to make s valid.
1 <= len(s) <= 1000s consists of '(' and ')' onlyInput: {"s":"())"}
Output: 1
One insertion fixes the trailing closer: "(())" or "()()".
Input: {"s":"((("}
Output: 3
Each unmatched opener needs its own closing bracket.