Java: Minimum Swaps for Bracket Balancing

Level : Intermediate
Mentor: Shailendra Chauhan
Type : GuidedLab
Points : 10
Duration : 00:30:00

Lab Details

Problem Statement:

You are given a string of 2N characters consisting of N ‘[‘ brackets and N ‘]’ brackets. A string is

considered balanced if it can be represented in the form S2[S1] where S1 and S2 are balanced

strings. We can make an unbalanced string balanced by swapping adjacent characters. Calculate

the minimum number of swaps necessary to make a string balanced.

Example

Input:

[]][][

Output:

2
First swap: Position 3 and 4
[][]][
Second swap: Position 5 and 6
[][][]
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this