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 :: c++ copy constructor 
Cpp :: phi function (n log (log(n))) 
Cpp :: insertion overloading in c++ 
Cpp :: floor and ceil in cpp 
Cpp :: assign value to a pointer 
Cpp :: c++ threadpool 
Cpp :: c++ main function parameters 
Cpp :: C++ switch..case Statement 
Cpp :: convert uppercase to lowercase 
Cpp :: c++ allocate dynamic with initial values 
Cpp :: remove duplicates from sorted list leetcode solution in c++ 
Cpp :: how to show constellations in starry night orion special edition 
Cpp :: online converter c++ to c 
Cpp :: enter items in array until enter is pressed c++ 
Cpp :: how to bath without water 
Cpp :: varint index 
Cpp :: print all variables separated by comma c++ 
Cpp :: how to type a vertical stack program c++ 
Cpp :: nmake.exe is not found in the windows 
Cpp :: c++ to mips converter online 
Cpp :: how to create a custom event in ue4 c++ 
Cpp :: transpose function example in c++ 
Cpp :: c++ terinary operator 
Cpp :: stl map 
Cpp :: how to run the code 
Cpp :: split 2d array into chunks in c++ 
Cpp :: using of and || c++ 
Cpp :: c++ sort cout end 
Cpp :: convert c program to c ++ online 
Cpp :: convert c program to c++ online 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =