Search
 
SCRIPT & CODE EXAMPLE
 

CPP

cpp valid parentheses

//AIGHT BRU, LESSGO!
//valid parantheses. or balanced parantheses
#include<iostream>
#include<stack>
using namespace std;
bool isvalid(string s)
{
	int n=s.length();
	bool ans=true;
	stack<char>st; // creating a stack to work with.
	for(int i=0;i<n;i++)
	{
		if(s[i]=='(' || s[i]=='{' || s[i]=='[' ) st.push(s[i]); // directly putting all opening brackets in.
		else if(s[i]==')')//check if there is a closing bracket on top of stack.
		{
			if(!st.empty() && st.top()=='(') st.pop();
			else 
			{
				ans=false;
				break;
			}
		}
		else if(s[i]==']')//check if there is a closing bracket on top of stack.
		{
			if(!st.empty() && st.top()=='[') st.pop();
			else 
			{
				ans=false;
				break;
			}
		}
		else if(s[i]=='}')//check if there is a closing bracket on top of stack.
		{
			if(!st.empty() && st.top()=='{') st.pop();
			else 
			{
				ans=false; // coz we can only have brackets and not other characters.
				break;
			}
		}
		else 
		return false;
	}
	if(!st.empty()) return false; // check if there are still some brackets (opening) left in stack.
	else return ans;
}
int main()
{
	string s;
	cin>>s;
	if(isvalid(s)) cout<<"yes";
	else cout<<"no";
}
Comment

valid parentheses in cpp

#include<iostream>
#include<stack>
using namespace std;
bool isValid(string s) {
	stack<char> st;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '(' || s[i] == '{' || s[i] == '[')
			st.push(s[i]);
		if (!st.empty())
		{
			if (s[i] == ')')
			{
				if (st.top() == '(')
				{
					st.pop();
					continue;
				}
				else
					break;
			}
			//
			if (s[i] == '}')
			{
				if (st.top() == '{')
				{
					st.pop();
					continue;
				}
				else
					break;
			}
			//
			if (s[i] == ']')
			{
				if (st.top() == '[')
				{
					st.pop();
					continue;
				}
				else
					break;
			}
		}
		else
			return false;
	}
	return st.empty() ? true : false;
}
int main()
{
	string s;
	cin >> s;
	cout << isValid(s) << "
";
	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to take input in 2d vector in c++ 
Cpp :: phi function 
Cpp :: public method 
Cpp :: Shell-Sort C++ 
Cpp :: C++ Pointers to Structure 
Cpp :: kadane algorithm with negative numbers included as sum 
Cpp :: How to see gateway on linux 
Cpp :: c++ comment 
Cpp :: queue 
Cpp :: kmp c++ 
Cpp :: sstream c++ 
Cpp :: c++ overloading by ref-qualifiers 
Cpp :: gcd of two numbers 
Cpp :: read a file line by line c++ struct site:stackoverflow.com 
Cpp :: Array declaration by specifying the size and initializing elements in C++ 
Cpp :: surf interpolation matlab 
Cpp :: use ster when declaring variables cpp 
Cpp :: Minimizing the dot product codechef in c++ 
Cpp :: c++ take n number from the user and store them in array and get the max, min number of them and also find the average/summation of these numbers 
Cpp :: vector and algorithm 
Cpp :: pallindrome string 
Cpp :: vowel and consonant program in c++ using if else 
Cpp :: is variable sized array are not allowed in c++? 
Cpp :: destiny child 
Cpp :: como copiar codigo de c++ con numeros de fila en docs 
Cpp :: go to particular place in vector using iterator 
Cpp :: sort vector in c 
Cpp :: 2000pp pp play osu std 
Cpp :: what is vector capacity in c++ 
Cpp :: cpp practice questions 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =