-
Notifications
You must be signed in to change notification settings - Fork 52
/
Valid Parentheses.cpp
45 lines (43 loc) · 1.51 KB
/
Valid Parentheses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*Created by Lakshay Goel
Github Profile link: https://github.com/MrLakshay
Question link: https://leetcode.com/problems/valid-parentheses/
Problem statement : Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Runtime: 0 ms O(n)
Memory Usage: 6.2 MB
Sample test case :
Your input: []
Output: true
Expected: true
Approach :
Receive input in the form of string.
Traverse the string and when opening bracket found push it into an vector if closing bracket of same type of vector last element is
found then pop the vector element and if not return false. If vector is empty after traversing whole string return true else false.
*/
class Solution {
public:
bool isValid(string s) {
vector <char> ch;
int n=s.size();
for(int i=0;i<n;i++){
if(s[i]=='(' || s[i]=='{' || s[i]=='[' ){
ch.push_back(s[i]);
}
else{
if(ch.empty())return false;
int z=ch.size();
if((s[i]==')'&&ch[z-1]=='(')||(s[i]==']'&&ch[z-1]=='[')||(s[i]=='}'&&ch[z-1]=='{')){
ch.pop_back();
}
else{
return false;
}
}
}
if(ch.empty())
return true;
return false;
}
};