Тёмный

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

codestorywithMIK
Подписаться 69 тыс.
Просмотров 9 тыс.
50% 1

Whatsapp Community Link : www.whatsapp.c...
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
My solutions on Github(C++ & JAVA) - github.com/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZ...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1: Using Stack
Time Complexity: O(n)
Space Complexity: O(n)
This approach uses a stack to keep track of unmatched opening brackets ([). When a closing bracket (]) is encountered, it checks if the stack is not empty, indicating a corresponding unmatched opening bracket exists. If so, it pops the stack, effectively pairing the brackets. At the end of the traversal, the remaining elements in the stack indicate unmatched opening brackets. The minimum number of swaps required to balance the string is calculated using the formula (stack.size() + 1) / 2.
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

Опубликовано:

 

9 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 60   
@bokthair4605
@bokthair4605 2 дня назад
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
@codestorywithMIK
@codestorywithMIK 2 дня назад
Thank you so much ♥️🙏
@gui-codes
@gui-codes 2 дня назад
Is level ka consistency being a working professional + you do sports + travelling + handling instagram etc. I have no idea how you manage your time
@Polly10189
@Polly10189 2 дня назад
Ye 2 bhai hai Judwa... Ak mze krta hai... ak LC krta hai 🤣
@AYJ959
@AYJ959 День назад
@@Polly10189 😂
@ugcwithaddi
@ugcwithaddi 2 дня назад
9:38 - This cleared everything. Thanks 👌🏻
@peterfromengland8663
@peterfromengland8663 2 дня назад
Hello haddi tumhari job lag gyi kya?
@coldcoke9254
@coldcoke9254 День назад
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
@MohammedHasmi577
@MohammedHasmi577 2 дня назад
Mik sir u r the greatest, aapki videos dekh ke khud se intuition smj aaja te hai , may allah pak bless you sir❤
@salmaniproductions1104
@salmaniproductions1104 2 дня назад
Thank you so much bhaiya. I hope aapke jo family member hospitalize hue the, wo ab behtar honge...
@Anmol-h8z
@Anmol-h8z День назад
Thanks bro Appreciate your work keep growing ❤
@raunakkumar1150
@raunakkumar1150 2 дня назад
Wow.... I liked your explanation
@Tab-i6c
@Tab-i6c 2 дня назад
I used the same two pointer approach as you explained in yesterday's problem to avoid stack space.
@manasuniyal2897
@manasuniyal2897 2 дня назад
Hello bro , you are great man
@codestorywithMIK
@codestorywithMIK 2 дня назад
♥️♥️🙏🙏
@it047_prakhar8
@it047_prakhar8 День назад
i solved this problem by just counting in O(N) time and O(1) space
@mehak_mits
@mehak_mits 2 дня назад
Great Explanation
@yereneager
@yereneager 2 дня назад
Apke hi LC k solution ko pdh k smjha tha video aane s pehle 😁
@Engineering.Wallah
@Engineering.Wallah 2 дня назад
LC id btao
@yereneager
@yereneager 2 дня назад
@@Engineering.Wallah Aaj ke question ke solutions me 3rd pe hi tha bhaiya ka soln most voted me
@aman_v3
@aman_v3 2 дня назад
Bhai id bataw
@priyanshkumar17
@priyanshkumar17 2 дня назад
@@yereneager Thanks for telling
@jk-sm6qr
@jk-sm6qr 2 дня назад
Thanks Mazhar Bhai
@rockykumarverma980
@rockykumarverma980 2 дня назад
Thank you so much Bhaiya 🙏🙏🙏
@Inspire_with_SM
@Inspire_with_SM 2 дня назад
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🙂
@growmoreyt4192
@growmoreyt4192 2 дня назад
Its not gonna miss by me if i am seeing this question again.
@parvahuja7618
@parvahuja7618 2 дня назад
apart from DSA, could you please give a tip on time management
@abhinay.k
@abhinay.k 2 дня назад
thanks
@raunakkumar1150
@raunakkumar1150 2 дня назад
Leetcode ke constest ka v solution upload kijiye. Bahut madad milegi
@RohitKumar-dz8dh
@RohitKumar-dz8dh 2 дня назад
Thanks 😊
@KuldeepKumar-rt8fs
@KuldeepKumar-rt8fs 2 дня назад
Great Bro
@gauravmundhada1647
@gauravmundhada1647 2 дня назад
MIK k sath pair bana k DSA set. Please guide how to identify patterns.
@ladipoge684
@ladipoge684 2 дня назад
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
@nishantdehariya5769
@nishantdehariya5769 2 дня назад
awesome
@Piyush_sahu.1
@Piyush_sahu.1 День назад
❤❤❤
@aamirsuhail9710
@aamirsuhail9710 2 дня назад
Sir how to improve aptitude for on campus placement?
@HeetVichhivora
@HeetVichhivora День назад
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 ...
@codestorywithMIK
@codestorywithMIK День назад
Yes i have discussed the O(1) space solution in this video too. Watch till end ♥️♥️♥️😇
@HeetVichhivora
@HeetVichhivora День назад
@@codestorywithMIK ohh apologies , thank you !!!
@sujayvikramgs8588
@sujayvikramgs8588 2 дня назад
take sql also pljj❤
@ugcwithaddi
@ugcwithaddi 2 дня назад
Yes finally 😊
@utkarshsahay9908
@utkarshsahay9908 2 дня назад
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; } };❤❤
@TheAI-Tutor
@TheAI-Tutor 2 дня назад
Instead of using stack, We can track the opened brackets via variables too! class Solution { public: int minSwaps(string s) { int open = 0, swaps = 0; for (char c : s) { if (c == '[') { open++; } else { open--; if (open < 0) { swaps++; open = 1; } } } return swaps; } };
@gui-codes
@gui-codes 2 дня назад
17:00 me bataya hai MIK ne
@hemant_kumar_071
@hemant_kumar_071 2 дня назад
Solved on my own !! class Solution { public: int minSwaps(string s) { int n = s.size(); int cnt = 0; int swapCnt=0; int right = n-1; int left = 0; while(s[right] == ']'){ right--; } while(left < right){ if(s[left] == '[') cnt++; else if(s[left] == ']') cnt--; if(cnt < 0){ cnt=1; swapCnt++; swap(s[left],s[right]); right--; } left++; } return swapCnt; } };
@anirbandhara8835
@anirbandhara8835 День назад
What is the need of using the variable ' cnt' ?
@ashishkashyap3242
@ashishkashyap3242 2 дня назад
almost same tha bs pattern nhi pakad paya
@aizad786iqbal
@aizad786iqbal 2 дня назад
aaj ka khud se kara tha two pointers se ... (hint leke ) class Solution { public int minSwaps(String s) { int open = 0; int close = 0; int n = s.length(); int i=0, j = n-1; int count = 0; StringBuilder sb = new StringBuilder(s); while( i < n){ char ch = sb.charAt(i); if(ch == '['){ open++; } if(ch == ']'){ close++; } if( close > open ){ char temp = sb.charAt(i); sb.setCharAt(i, sb.charAt(j)); sb.setCharAt(j, temp); close--; open++; count++; } i++; } return count; } }
@yereneager
@yereneager 2 дня назад
Great, but with this logic we dont even need to swap them. You can remove them
@adarshkumargupta9751
@adarshkumargupta9751 2 дня назад
Why adding 1?
@priyanshkumar17
@priyanshkumar17 2 дня назад
to handle odd value of n/2
@sujayvikramgs8588
@sujayvikramgs8588 2 дня назад
les goo
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 дня назад
First 🎉❤
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 дня назад
please make video of leetcode weekly contest qn no.3and 4 so good problem 🎉❤
@codestorywithMIK
@codestorywithMIK 2 дня назад
Dear Shankar, I will definitely take out time for that. Actually one of my family members will be undergoing a critical surgery tomorrow and hence I will a little occupied this week. Hope you all will understand ♥️🙏
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 дня назад
@codestorywithMIK pray for your relative recover soon and best wishes for your family members 🙏 ❤️
@priyanshkumar17
@priyanshkumar17 2 дня назад
@@codestorywithMIK take care of your family member MIK... 🙏 ❤
@mahalakshmi.m7994
@mahalakshmi.m7994 2 дня назад
Bro can you please upload videos in english so it will be very useful...pls consider
@codestorywithMIK
@codestorywithMIK 2 дня назад
Dear Mahalakshmi, I will definitely plan to make a separate channel in which I will post same video dunned in Hindi. Due to crunch of time, I am currently only uploading in Hindi. Hope you understand Thank you ♥️🙏
@mahalakshmi.m7994
@mahalakshmi.m7994 День назад
​@codestorywithMIK thank you so much for that ...waiting 🎉
Далее
LeetCode was HARD until I Learned these 15 Patterns
13:00
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Просмотров 250 тыс.
Maximum Width Ramp - Leetcode 962 - Python
14:47
Просмотров 7 тыс.