forked from vikasgond807/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vikasgond807#12 from Aditi2905/main
Valid parentheses code
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
bool isValid(string x) | ||
{ | ||
stack <char> st; | ||
for(int i=0;i<x.length();i++) | ||
{ | ||
if(st.empty()) st.push(x[i]); | ||
else | ||
{ | ||
if(x[i]=='(' || x[i]=='{' || x[i]=='[') st.push(x[i]); | ||
else if(x[i]==']' && st.top()=='[') st.pop(); | ||
else if(x[i]=='}' && st.top()=='{') st.pop(); | ||
else if(x[i]==')' && st.top()=='(') st.pop(); | ||
else return false; | ||
} | ||
} | ||
if(st.empty()) return true; | ||
return false; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
string s="{()}[]"; | ||
if(isValid(s)) cout<<"Valid"; | ||
else cout<<"Invalid"; | ||
} |