It was a question on coursera course in Data structure and I spent some time on the program. Though I had taught the program to students earlier.
But somehow coursera is not accepting my submission:( So why not blog it?
The question is Write a program to balance brackets using stack. This is fundamental requirement in any programming language.
Let us look at some valid and invalid strings.
{[]}() valid
[]{}()[] valid
{([([])])} valid
{}) invalid
([)] invalid
({[]} invalid
You are getting the gist, right? When ever there is an opening bracket, there should be a corresponding closing bracket of matching type. And order is important, you can not have round bracket , square bracket followed by closing round bracket first and square bracket next.
So the algorithm is very simple
Now for the code. I used the stack from stl in c++. Here is the code.
Not so tough? Isn't it?
For more such questions and notes on all advanced topics, download my app c++ notes
But somehow coursera is not accepting my submission:( So why not blog it?
The question is Write a program to balance brackets using stack. This is fundamental requirement in any programming language.
Let us look at some valid and invalid strings.
{[]}() valid
[]{}()[] valid
{([([])])} valid
{}) invalid
([)] invalid
({[]} invalid
You are getting the gist, right? When ever there is an opening bracket, there should be a corresponding closing bracket of matching type. And order is important, you can not have round bracket , square bracket followed by closing round bracket first and square bracket next.
So the algorithm is very simple
- create a stack of characters
- repeat for each i from 0 to length of string
- ch = ith character
- if ch is either { or [ or (, push it to stack
- if ch is closing bracket i.e. } or ] or }
- get stack top charcter ch2
- if ch and ch2 are of same type pop the char
- if not return a false
- if stack is empty all opening brackets are popped. Return true
- if stack is not empty, there are some extra opening brackets. Return false
Now for the code. I used the stack from stl in c++. Here is the code.
#include<stack>
#include<iostream>
using namespace std;
bool isBalanced(string str,char &wrongChar);
int main()
{
string str;
cout<<"Enter your string";
getline(cin,str,'\n');
char ch;
bool bal = isBalanced(str,ch);
if(bal)
cout<<"success";
else
cout<<ch;
}
bool isBalanced(string str,char &wrongChar)
{
stack<char> brackStack;
int len = str.length();
for(int i=0;i<len;i++)
{
char ch= str[i];
if(ch=='(' || ch=='[' || ch=='{')
brackStack.push(ch);
else if ( ch==')' || ch==']' || ch=='}')
{
char topChar = brackStack.top();
if( (ch==')' && topChar=='(') ||
(ch==']' && topChar=='[') ||
(ch=='}' && topChar=='{') )
{
brackStack.pop();
continue;
}
else{
/* not matching*/
wrongChar = ch;
return false;
}
}
}
if(brackStack.empty())
return true;
else
{
wrongChar = brackStack.top();
return false;
}
}
Not so tough? Isn't it?
For more such questions and notes on all advanced topics, download my app c++ notes
Comments
Post a Comment