Minimum Add to Make Parentheses Valid | Simple Intuition | Leetcode 921 | codestorywithMIK

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

In this video we will try to solve an easy stack based Problem : Minimum Add to Make Parentheses Valid | Simple Intuition | Leetcode 921 | 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 Add to Make Parentheses Valid | Simple Intuition | Leetcode 921 | codestorywithMIK
Company Tags : META

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

Summary :
Stack-Based Approach:

Logic: Uses a stack to track unmatched opening parentheses (. For each closing parenthesis ), if there is a corresponding opening parenthesis in the stack, it is popped; otherwise, a counter (open) is incremented for unmatched closing parentheses.
Complexity:
Time: O(n)
Space: O(n) (due to stack usage)
Counter-Based Approach:

Logic: This approach uses two integer counters: size to track unmatched opening parentheses and open to count unmatched closing parentheses. It iterates through the string, incrementing size for each ( and decrementing it for each ) if there are unmatched opening parentheses (size greater than 0). If there are no unmatched opening parentheses, it increments open.
Final Calculation: The result is open + size, representing the total number of parentheses needed to balance the string.
Complexity:
Time: O(n)
Space: O(1) (constant space, as it only uses integer variables)

✨ 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
Рекомендации по теме
Комментарии
Автор

I saw your previous videos and I was able to solve it using stack and variable approach both. Your way of explanation is really easy and simple. Thanks MIK.

Ajaypatel-oyol
Автор

nice explanation solved after watching 5 min!!

NandakishorGudala
Автор

Same as yesterday's potd..with litfle change😊..your yesteddays solution always helps

codewithnode
Автор

Solved it by own, Thank you so much bhaiyya for previous videos

salmaniproductions
Автор

Wish me best luck . I'm starting DSA from scratch kyuki aaj array ke question me hug ke aaya

peterfromengland
Автор

Sir I have watch your yesterday's video and I was able to solve by myself and now come to show your amazing approach 🎉❤

MohammedHasmi
Автор

your previous video really helped me thank you for that...

azeevlogs
Автор

class Solution {
public:
int minAddToMakeValid(string s) {
int count=0;
int n=s.size();
stack<int> st;
for(int i=0;i<n;i++){
if(s[i]=='('){
st.push(s[i]);
}else{
if(st.empty()){
count++;
}else{
st.pop();
}
}
}
return count+st.size();

}
};

HARSHRAJ-wzrp
Автор

Please start solving problems on CodeForces also.

gauravmundhada
Автор

Aaj problem dekha ki kal jo apne explain kiya tha vaise hi hai

kartikkaushik
Автор

Sir, can you please upsolve the contest problems, it will be great for us.

RajatSingh-xuto
Автор

Solved on my own !!!
class Solution {
public:
int minAddToMakeValid(string s) {

stack<char> st;

int openBracketCnt = 0;
for(char ch : s){

if(!st.empty() && ch == ')' && st.top() == '(' ){
st.pop();
}

//It means there should be an '(' to balance this ')'
// so increment the count of OpenBracket '('
else if(st.empty() && ch == ')') openBracketCnt++;

else if(ch == '(') st.push(ch);

}

//After traversing all characters either your stack is empty or
// have some '(' Opening Brackets,
// if empty means it is balanced, just return openBracketCnt;
// else you need same number of ')' (closing bracket) to
// balance '(' (Opening Bracket) return OpenBracketCnt + st.size()

return openBracketCnt + st.size();
}
};

hemant_kumar_
Автор

tbh i solve all similar question of leetcode daily. so kal hi karlia tha ye... cheerzs

expanse
Автор

:) Python

class Solution:
def minAddToMakeValid(self, s: str) -> int:

st = []
st.append(s[0])
i = 1
while i < len(s) :
if (st and st[-1] == '(' and s[i] == ')') :
st.pop()
else :
st.append(s[i])
i += 1
return len(st)

Arjun-bsvl
Автор

please fix the mike issue(so overall quality will be🔥),
content is already 🔥

om
Автор

Hi Mazhar. Thanks for the nice and easy to understand explanation.

This could be a noob question. But it'd help if somebody could provide an explanation to this. When I submitted the solution using Stack approach, it didn't beat 100% users and had O(N) complexity. Is it okay if our leetcode solution doesn't always beat 100% of users?

rnehacodes
Автор

Bhaiya yeh codechef ke questions nahi ban rahe hai in contest. Any advice on how to improve on that?

rohitaggarwal
Автор

Bhaiya Good Morning, kaise ho aap and aapke family ka health kaisa hai ab, I pray for him/her😊

learn_in_shorts
Автор

Bhaiya I need your help . Actually I am from non tech background but I love to code and I want to know that besides DSA and development which subjects should I need to learn & from where and should I also need to learn system design for Google Microsoft

itsharshita
Автор

Solve with the help of your yesterday video.
class Solution {
public:
int minAddToMakeValid(string s) {
stack<char>st;
if(s.size()==0) return 0;
for(char &ch :s)
{
if(ch=='(')
{
st.push(ch);
}
else if(!st.empty() && ch==')' && st.top()!=')')
{
st.pop();
}
else
{
st.push(ch);
}
}
return st.size();

}
};

poojakapri
visit shbcf.ru