Minimum Number of Swaps to Make the String Balanced | Reason | Leetcode 1963 | codestorywithMIK

preview_player
Показать описание
This is the 23rd Video of our Playlist "Stack : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve an easy stack based Problem : Minimum Number of Swaps to Make the String Balanced | Detailed Reason | Leetcode 1963 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Minimum Number of Swaps to Make the String Balanced | Detailed Reason | Leetcode 1963 | codestorywithMIK
Company Tags : will update soon

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
Approach 1: Using Stack

Time Complexity: O(n)
Space Complexity: O(n)

Pros: Simple to understand, directly simulates bracket pairing.

Cons: Uses extra space proportional to the number of unmatched brackets.

Approach 2: Without Using Stack

Time Complexity: O(n)
Space Complexity: O(1)
Instead of using a stack, this approach uses a counter (size) to keep track of unmatched opening brackets. For every [ encountered, the counter is incremented, and for each ] encountered, it is decremented if the counter is non-zero (indicating an unmatched opening bracket is available). After processing the entire string, the remaining value in size represents the number of unmatched opening brackets. The number of swaps is calculated as (size + 1) / 2.

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

00:03 Explaining the concept of Minimum Number of Swaps to Make the String Balanced
00:56 Minimum swaps needed to balance a string
02:33 Zero swaps required if input string is already balanced
03:22 Focus on balancing the string with minimum swaps
05:07 Finding minimum swaps needed to balance the string
06:02 Calculate minimum number of swaps for string balance
07:53 Balancing a string has a predictable pattern
08:43 Balancing pairs by making swaps
10:20 Learn how to balance strings by minimum swaps
11:12 Need only 2 swaps to balance brackets
12:51 Handling balanced parenthesis using stack
13:29 Explanation of balancing brackets using stack data structure
15:00 Minimum swaps needed to balance a string.
15:45 Balancing opening and closing brackets using a stack
17:29 Stack size manipulation and solving for optimal space

bokthair
Автор

Is level ka consistency being a working professional + you do sports + travelling + handling instagram etc.
I have no idea how you manage your time

gui-codes
Автор

9:38 - This cleared everything. Thanks 👌🏻

ugcwithaddi
Автор

Mik sir u r the greatest, aapki videos dekh ke khud se intuition smj aaja te hai, may allah pak bless you sir❤

MohammedHasmi
Автор

keep going sir!! You are awesome!! Aaj ka almost kr liya tha bas pattern galat figure out kiya tha kyuki pen paper nhi use kr rha tha

coldcoke
Автор

Thank you so much bhaiya. I hope aapke jo family member hospitalize hue the, wo ab behtar honge...

salmaniproductions
Автор

Thanks bro Appreciate your work keep growing ❤

Anmol-hz
Автор

I used the same two pointer approach as you explained in yesterday's problem to avoid stack space.

Tab-ic
Автор

Apke hi LC k solution ko pdh k smjha tha video aane s pehle 😁

yereneager
Автор

i solved this problem by just counting in O(N) time and O(1) space

it_prakhar
Автор

Its not gonna miss by me if i am seeing this question again.

growmoreyt
Автор

bhaiya easy hai aaj ka problem kal se bhi easy hai, bas thoda sa observation ki jarurat hai, ye bhi easy marked ho sakta tha but wah observation ke liye medium marked hai shayad🙂

Inspire_with_SM
Автор

apart from DSA, could you please give a tip on time management

parvahuja
Автор

Leetcode ke constest ka v solution upload kijiye. Bahut madad milegi

raunakkumar
Автор

MIK k sath pair bana k DSA set.
Please guide how to identify patterns.

gauravmundhada
Автор

Hello MIk,
A kind of suggestion from my side ur explanation is too good building intuition from the scratch is very nice why can't you add a kind of section in ur video or any other videos like for suppose kind of thing in which for example in the above question they asked minimum swaps its fine in that place if question was like to give the balanced string after swaps why can't you make a video like this for suppose

ladipoge
Автор

(n+1)/2 is equivalent to ceil(n/2)
or in general ceil(n/d)=(n+d-1)/d

parthokr-ol
Автор

Hello Mik sir
I hope you and the family member are doing well
I had a request that please also discuss the optimal soln for this problem which is given in leetcode hints which doesn't use space

I hope this comment reaches you

Thank you ...

HeetVichhivora
Автор

Sir how to improve aptitude for on campus placement?

aamirsuhail
Автор

class Solution {
public:
int minSwaps(string s) {
int i = 0;
int j = s.size()-1;
int ans = 0;

int cnto = 0;
int cntc = 0;

while(i < j){
if(s[i] == '['){
cnto++;
}
else{
cntc++;
}
if(cntc > cnto){
while(i < j && s[j] != '['){
j--;
}
swap(s[i], s[j]);
ans++;
cntc--;
cnto++;
}
i++;
}
return ans;
}
};❤❤

utkarshsahay