Reverse Substrings Between Each Pair Of Parentheses | Leetcode 1190 | Stack

preview_player
Показать описание

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. 1: You are given a string s that consists of lower case English letters and brackets.
2: Reverse the strings in each pair of matching parentheses, starting from the innermost one.
3: Your result should not contain any brackcet

Topic: #Stack #Queue #String

Used #DataStructure: #Stack #Queue #String #Array

--------------------------------------------------------------

Linked Questions:

--------------------------------------------------------------

Smimilar Questions:

---------------------------------------------------------------

----------------------------------------------------------------

#Stack #Queue #String #Array #Leetcode1190
Рекомендации по теме
Комментарии
Автор

good approach. I had a similar approach but I was always started the operations with opening bracket instead of closing bracket which makes sense if you are reversing. thanks for the insight

ShivaM-dcxe
Автор

example == bnda "bnda" is like your favourite word dude..Well good videos . thanks

ankit
Автор

Sir, I think the Time Complexity of the stack/queue approach will be O(n^2) as -
for: string = outer(inner)outer
string inner can also have parenthesis
So T(n) = T(n-2){for the inner string to resolve all its parenthesis} + 2*n{for reversing the whole string}
=> T(n) = T(n-2) + 2*n
=> T(n) = T(n-4) + 2*(n-2) + 2*n
=> T(n) = T(n-6) + 2*(n-4) + 2*(n-2) + 2*n and so 1
=> T(n) ~ 2*(sum of all even numbers till n)
=> T(n) = n(n+2)/2
=> T(n) ~ O(n^2)

DS-bgrx
Автор

Sir, can you please make a video on O(n) solution of this question, thanks!

artistic__
Автор

Sir instead of Queue, why can't we make use of an empty string and putting the popped characters into it?

architsharma
Автор

class Solution {
public:
string reverseParentheses(string s) {
int n=s.length();
stack<int>st;
for(int i=0;i<n;i++)
{
if(s[i]=='(')
{
st.push(i);
}
else if(s[i]==')')
{
int l=st.top()+1;
st.pop();
int r=i-1;
while(l<r)
{
swap(s[l], s[r]);
l++;
r--;
}
}
}
string res="";
for(int i=0;i<n;i++)
{
if(s[i]!=')'&&s[i]!='(')
{
res+=s[i];
}
}
return res;

}
};
o(n) solution discussed in last part.

yashbansal
Автор

can u tell me if i am inserting first why my answer is not comming

but if i am inseting in else part it gives me right answer

letsdoeverythinginoneweek
Автор

total time complexity kya hai pura question ka?

architsinha