Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Swaps to Make the String Balanced

med
30 min
Try to solve the Minimum Number of Swaps to Make the String Balanced problem.

Statement

Given a string s consisting only of the characters '[' and ']', you may perform swaps of any two characters in s. Return the minimum number of swaps required to transform s into a balanced bracket string.

A string is balanced if it can be read from left to right such that, at every position, the number of '[' seen so far is at least the number of ']' seen so far, and the total counts of '[' and ']' are equal.

Constraints:

  • n == s.length

  • 22 \leq n 106\leq 10^6

  • n is even

  • s[i] is either '[' or ']'

  • The number of '[' in s equals n / 2, and the number of ']' in s equals n / 2

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Swaps to Make the String Balanced

med
30 min
Try to solve the Minimum Number of Swaps to Make the String Balanced problem.

Statement

Given a string s consisting only of the characters '[' and ']', you may perform swaps of any two characters in s. Return the minimum number of swaps required to transform s into a balanced bracket string.

A string is balanced if it can be read from left to right such that, at every position, the number of '[' seen so far is at least the number of ']' seen so far, and the total counts of '[' and ']' are equal.

Constraints:

  • n == s.length

  • 22 \leq n 106\leq 10^6

  • n is even

  • s[i] is either '[' or ']'

  • The number of '[' in s equals n / 2, and the number of ']' in s equals n / 2

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths