665. Minimum Swaps to Make String Balanced

HardStackStackGreedyString

Given a string s of equal numbers of '[' and ']', in one swap you may exchange any two characters. Return the minimum number of swaps to make s a balanced bracket string. The input is JSON {s}.

Input: JSON {s}.

Output: Integer — the minimum swaps.

Examples

Example 1
Input: {"s":"][]["}
Output: 1
Explanation: One swap balances it.
Example 2
Input: {"s":"]]][[["}
Output: 2
Explanation: Two swaps needed.
Example 3
Input: {"s":"[]"}
Output: 0
Explanation: Already balanced.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →