Great explanation by sir, one correction I would like to add, is to only push "(" and operators in the stack and not the characters. The reason is (a+ (b)) is a redudant case. Happy coding! :)
What about string "(a)"? This should return 1(redundant braces). But with this algo, it will return 0(no redundant braces). Can you please explain how do we handle this case?
class Solution: def braces(self, A): stack=[] for i in A: if i==')': flag=True top=stack[-1] stack.pop() while(top!='('): if(top=='+' or top=='-' or top=='*' or top=='/'): flag=False
top=stack[-1] stack.pop() if flag==True: return 1 else: stack.append(i) return 0
@@jaydipkansara1 You can use sir's approach. Just one change should be made, i.e., use a flag variable before the while loop, that can check if there are any operators in between two brackets or not. Initialize the flag to false, and even after coming out of the while loop, if the flag is false, this means that there are no operators. So, we can understand that here redundancy is present.
what would be the output for (a).. i think your code will accept this. since there is duplicity in this input, there is no need to put parenthesis for single character
Hi In pep coding IDE I am submitting Python code, its not giving output.Can you please share the format which needs to submit. Do we need to define class or direct method we can add in python. Thank you for nice explanation.
ok i got the answer that is redudant because ((a+b)(c+d)) == (a+b)(c+d)we dont need outer brackets thats ehy this is correnct we get the same anwer without the outer bracs more examples down Input: ((a+b)) (a+(b)/c) (a+b*(c-d)) Output: Yes Yes No Explanation: 1. ((a+b)) can reduced to (a+b), this Redundant 2. (a+(b)/c) can reduced to (a+b/c) because b is surrounded by () which is redundant. 3. (a+b*(c-d)) doesn't have any redundant or multiple brackets.
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. If you like our efforts, we request a review g.page/Pepcoding/review?rc You can subscribe to our channel here ruclips.net/user/Pepcodingabout?view_as=subscriber
okay, I found the answer for this question. while popping until opening bracket found, we will count the number of operators(+,-,*,/). if the number of operators is 0, then we can say that it has duplicates inside the sub expression. i.e.((a)+b)
Thankyou beta! I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem. If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
han yar, DP karne lag gae. DP -> Graphs -> Strings -> OOPs -> Time and Space abhi yahan se kijie www.pepcoding.com/resources/data-structures-and-algorithms/strings-and-stringbuilder
sir your code not work for this (a+(b)/c) this test case is failing. since there is no use of () for b . bool RedundantBrackets(string s) { stack st; int count; for(int i=0; i
phela wala test case ma isliya false aaya kyu ki vo jo plus ha na vo bhi pop ho gaya kyu ki vo bracket ka beech ma ha isliya dusra vala fail hua kyu ki vo plus pop nahi ho payaaaaa
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. If you like my efforts, I request a review g.page/Pepcoding/review?rc 2nd step series will come here www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
I haven't come across a better explanation than this. Came after watching 2 other videos and reading one solution approach also
amazing teacher.. full Indian style teaching.. thank u
Great explanation by sir, one correction I would like to add, is to only push "(" and operators in the stack and not the characters. The reason is (a+ (b)) is a redudant case. Happy coding! :)
One of the best teachers on RUclips nicely explained
this is my first video ever seen in your channel you are GOAT(Greatest of all teachers )
I will be commenting on those videos which I have watched, in order to mark it. Great explanantion.....
samaj geya aur mein ho geya mala mala....thanks for the great explanation.
greatest of all time teaching
Sir..nice explanation!. But, for "()", we need to add some additional checks.
jo kabhi khula hi nai uske liye kya hi khojega bhai
Sir your teaching style is awesome👍👍👍👍
Very nice explanation sir !!
But...I think ,u missed this (a) kind of test case...!!
no these cases are working
You are just awesome sir !! 👌👌
And so are you. We are both awesome.
@@Pepcoding sir mai bhi hu😥😓
We have to look for a condition. at line 15 if stack is empty other wise it will throw EmptyStackException
Thank you sir. Very helpfull video...
Sir aap log toh bhot aaram se Amazon , Facebook ya Google ke interview fod ke aa jate honge 😍😍
a+b) ye karne pr exception dedega empty stack exception
Happy to do the solution by myself
Great explanation as always
What about string "(a)"? This should return 1(redundant braces). But with this algo, it will return 0(no redundant braces).
Can you please explain how do we handle this case?
they are not redundant. they have an expression inside.
@@Pepcoding Hi sir, thanks for replying. I tried this solution on interviewbit. But this case "(a)" is failing there.not sure how to handle this
class Solution:
def braces(self, A):
stack=[]
for i in A:
if i==')':
flag=True
top=stack[-1]
stack.pop()
while(top!='('):
if(top=='+' or top=='-' or top=='*' or top=='/'):
flag=False
top=stack[-1]
stack.pop()
if flag==True:
return 1
else:
stack.append(i)
return 0
@@jaydipkansara1 You can use sir's approach. Just one change should be made, i.e., use a flag variable before the while loop, that can check if there are any operators in between two brackets or not. Initialize the flag to false, and even after coming out of the while loop, if the flag is false, this means that there are no operators. So, we can understand that here redundancy is present.
Sir, but it's failing cases like (a) where it should be false it's showing true
what would be the output for (a).. i think your code will accept this. since there is duplicity in this input, there is no need to put parenthesis for single character
Broo itss okay as per sample test case too. Duplicacy will be for ((a))
The NDOS website is not running and not able to open it.
Sir agar ham space bhi use karte hai string mai
Space bhi push hoga kya stack pe??
Aur agar stack mai (,space hoga to peek se ( milega ya space??
space bhi push hoga
Sir this code is failing in gfg practice compiler.
Sir aap kis ide ko use krte h ??
very nice explanation
Thanks for liking
Is this question available on NADOS?
Hi In pep coding IDE I am submitting Python code, its not giving output.Can you please share the format which needs to submit. Do we need to define class or direct method we can add in python.
Thank you for nice explanation.
I will ask the dev team to check python compiler
This solution wont work in case of (a+b+(c)) (on gfg) but perfect explaination you yourself get to the solution after hitting that case
good sir..
but sir this is not working for (a+(b)/c)
it should print true
but its's printing false
Nice teaching sir 🙏🙏🙏✍️🙏🙏✍️
Thanks and welcome
Sir what about if the case is ((a+b)(c+d))
i am also confuse in that case
ok i got the answer that is redudant because ((a+b)(c+d)) == (a+b)(c+d)we dont need outer brackets thats ehy this is correnct we get the same anwer without the outer bracs
more examples down
Input:
((a+b))
(a+(b)/c)
(a+b*(c-d))
Output:
Yes
Yes
No
Explanation:
1. ((a+b)) can reduced to (a+b), this Redundant
2. (a+(b)/c) can reduced to (a+b/c) because b is
surrounded by () which is redundant.
3. (a+b*(c-d)) doesn't have any redundant or multiple
brackets.
Nice explanation sir
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
ruclips.net/user/Pepcodingabout?view_as=subscriber
@Pepcoding Sir you're not checking cases for (a) or (+)
it giving correct answers for these cases
what about a+(b)+c
sir inh dono line mai difference kya hota hai
String str = scn.next();
String str = scn.nextLine();
beta jaise a b c
hai to next aapko sirf a dega
nextLine a b c dega poora
next reads only a word
while nextLine reads whole line
(b+c-d+(c)) in this case the logic will be wrong...please try again
Sir you're not checking cases for (a) or (+)
sir apke course me question to he lkn oops aur baki chhote chhote concepts gayab he
what if we want to check duplicates inside sub expression??
okay, I found the answer for this question.
while popping until opening bracket found, we will count the number of operators(+,-,*,/).
if the number of operators is 0, then we can say that it has duplicates inside the sub expression.
i.e.((a)+b)
@@thisisdenish Or you can just counter the characters inside it..if its < 2..its redundant
Can anyone tell me why we are using return when this is clearly not a function why not use break to get out the loop ?
after printing true , it will return from the main function . so it won't execute any further statement
Sir you are awesome
Thankyou beta!
I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
sir the code fail for (a+(b)/c)
sir strings ki video aai hi ni hai to thodi dikkat ho rhi hai, agr ho to upload kr dijiyega.
Thankyou.
han yar, DP karne lag gae. DP -> Graphs -> Strings -> OOPs -> Time and Space
abhi yahan se kijie
www.pepcoding.com/resources/data-structures-and-algorithms/strings-and-stringbuilder
@@Pepcoding Thankyou so much sir..bhut khushi ho rhi hai thankyou thankyou thankyou...
arey, love you love you love you. Bhavuk kyu ho rha hai bro
@@Pepcoding 😁😁😅
Sir you're not checking cases for (a) or (+)
BESTTTTT
Nice
i was think the same login but in a different way
Thankyou sir
You're Welcome
sir your code not work for this
(a+(b)/c) this test case is failing.
since there is no use of () for b .
bool RedundantBrackets(string s)
{
stack st;
int count;
for(int i=0; i
phela wala test case ma isliya false aaya kyu ki vo jo plus ha na vo bhi pop ho gaya kyu ki vo bracket ka beech ma ha isliya dusra vala fail hua kyu ki vo plus pop nahi ho payaaaaa
May be your code fails in (a+(b) /c)
Plz reply sir
no its working
Your DSA series is amazing sir..Can you also list the questions of leetcode for Second step that is for level up..
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like my efforts, I request a review
g.page/Pepcoding/review?rc
2nd step series will come here
www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/
@@Pepcoding Thanks a lot Sir. I have dropped my review about the course..
Sir
(a + b) +((c + d))
Ye fail ho rha hai ek case c++ mai ismai aapne spaces diye hai spaces hatake pass ho rha hai can u please chk it🙏
Right!
Bro it's simple if input[i]==' ',then don't push it into the stack.
@@ankurkumar8465 still not getting the correct ans!
@@pranjalmittal1973 If input[i] = ' ' i. e. space then simply continue... don't do anything in this case.
@@ankurkumar8465 yeah, i did this, still it did not pass all the test cases.
Sir ye beech mai return lga k program terminate kese hora h, plz reply anyone
This will return trur for - (), but there is no duplicate brackets here
Want to buy your course for placements
Beta, you can contact us on 01140194461 Or you can write to us at contact@pepcoding.com
Son, I can give you my id for 12k only (non negotiable -__-)
Sir the code will fail for "(a)" test case.
use this condition:
if (A[i] == '(' && A[i + 2] == ')')
return 1;
Sir for () a+b it gives true.but we are talking about duplicate bracket
Lemme check it and go through it once again.
obiously it will give true cause its a duplicate bracket
Sir humne isme wo question jis ko hum test kar rhe the uska input kaha par diya?
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
(a+(b)/c) on this test case i think the given solution will not work please check
Gimme some time, will check and correct it, if it wrong.
sir appke first input mein spaces hain input: (a + b) + ((c + d)). So isliye mera first test case fail ho rha hai..
aap logic mei handle kar dijie ki space ignore ho jaien.
@@Pepcoding Thank you sir for your videos.. These are really helpful..
Incase if you are using sc.next() use sc.nextLine(). This worked for me.
@@harshtamkiya8505 Thanks but actually I am doing in c++ so this will not work 😅
sir how an i get the solution in cpp
For all your doubts refer to nados.pepcoding.com
Also stay tuned on our Instagram .
Sir video or dal dena java ki
int checkRedundancy(string s) {
// code here
stack st;
int i=0;
while(i
Ek Test case fail ho rha hai sir
(a+b)+((a+b)) espe
Isme duplicacy hai
bhai shi kam kr ra h
Java Solution
import java.util.* ;
import java.io.*;
import java.util.ArrayList;
public class Solution
{
public static boolean findRedundantBrackets(String s)
{
// Write your code here.
Stack stack = new Stack();
for(int i =0;i
1:string=(a+b)+(c+d)
2:string=(a+b*c*(d*f)+g)
could someone plz tell me about the o/p of this i/p?
I think o/p must be true
@pepcoding sir what is the difference between redundant brackets and duplicate brackets?
KUCH TEST CASE SAHI NAHI HA SAHI OUTPUT PA BHI WRONG ANSWER AARA
public static boolean checkRedundantBrackets(String expression) {
int n=expression.length();
Stack st=new Stack();
int i=0,count=0;
for(i=0;i
sign up on nados.io
You will get better experience with precisely organised content
if string is "))))))))"
good test case, just check if first char is ')' then return true.
10 rs ki Pepsi apna bhai sexy 😁😁😁😁
#include
using namespace std;
int main()
{
string s;
cin>>s;
stackst;
int c=0;
for(int i=0;i
Instead of if(c==0) to if(c==1) than problem is solved
maal hai ya maal nahi hain :p
Sir you're not checking cases for (a) or (+)