Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Valid Parentheses leetcode in c++

#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 rotate a matrix 90 degrees clockwise 
Cpp :: memcpy in cpp 
Cpp :: fractional knapsack problem 
Cpp :: abs in c++ used for 
Cpp :: memset function in c++ 
Cpp :: declare class c++ 
Cpp :: find second largest number in array c++ 
Cpp :: malloc 2d array cpp 
Cpp :: c++ char 
Cpp :: Write a C++ program using constructor 
Cpp :: 83. remove duplicates from sorted list solution in c++ 
Cpp :: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 
Cpp :: c++ method name 
Cpp :: using-controller-and-qt-worker-in-a-working-gui-example 
Cpp :: prefix using stack 
Cpp :: unambiguous 
Cpp :: c++ solver online free 
Cpp :: find no of occurences of each letter in string c++ 
Cpp :: what is c++ 
Cpp :: ++m in c 
Cpp :: define for loop c++ 
Cpp :: c++ text between substrings 
Cpp :: . Single-line comments start with two forward slashes (//). 
Cpp :: the question for me 
Cpp :: file transfer socat 
Cpp :: comment installer boost c++ sur windows 
Cpp :: c++ abs template 
Cpp :: free pair c++ 
Cpp :: c++ Unable to get CMake Tutorial example to compile 
Cpp :: do c++ ints neeed to be initlaized 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =