Skip to content

Commit f58583a

Browse files
committed
valid parenthesis code
1 parent 76118e6 commit f58583a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 162 |
9+
| Total Problems | 163 |
1010

1111
</center>
1212

@@ -162,6 +162,7 @@ Include contains single header implementation of data structures and some algori
162162
| :------------ | :----------: |
163163
| We have series of n daily price quotes for a stock. We need to calculate span of stock's price for all n days. Span for ith day is defined as maximum number of consecutive days, for which the price of the stock was less than or equal to ith day. For stock quotes {100, 60, 70, 65, 80, 85} span will be {1, 1, 2, 1, 4, 5}. Span for day 1 is always 1, now for day 2 stock is at 60, and there is no day befor it when stock was less than 60. So span remains 1. For day 3, the stock is priced at 70, so its span is 2, as previous day it was 60, and so on. | [stock_span_problem.cpp](stack_problems/stock_span_problem.cpp) |
164164
| Given an infix expression, convert it to postfix expression, Example (A+B)\*C --> AB+C\* | [infix_to_postfix.cpp](stack_problems/infix_to_postfix.cpp) |
165+
| Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.| [valid_parenthesis.cpp](stack_problems/valid_parenthesis.cpp)|
165166

166167
### Sort and Search Problems
167168
| Problem | Solution |

stack_problems/valid_parenthesis.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Given a string containing just the characters '(', ')', '{', '}',
3+
* '[' and ']', determine if the input string is valid. The brackets must
4+
* close in the correct order, "()" and "()[]{}"
5+
* are all valid but "(]" and "([)]" are not.
6+
*/
7+
8+
#include <iostream>
9+
#include <stack>
10+
11+
bool is_valid_parenthesis(std::string str)
12+
{
13+
std::stack<char> st;
14+
for (char c: str) {
15+
if (c == '(') {
16+
st.push(')');
17+
} else if (c == '{') {
18+
st.push('}');
19+
} else if (c == '[') {
20+
st.push(']');
21+
} else if (!st.empty()) {
22+
if (st.top() != c) {
23+
return false;
24+
} else {
25+
st.pop();
26+
}
27+
}
28+
}
29+
return st.empty();
30+
}
31+
32+
int main()
33+
{
34+
std::string str1{"()[]{}"};
35+
if (is_valid_parenthesis(str1)) {
36+
std::cout << str1 << " has valid parenthesis." << std::endl;
37+
} else {
38+
std::cout << str1 << " does not have valid parenthesis." << std::endl;
39+
}
40+
std::string str2{"([)]"};
41+
if (is_valid_parenthesis(str2)) {
42+
std::cout << str2 << " has valid parenthesis." << std::endl;
43+
} else {
44+
std::cout << str2 << " does not have valid parenthesis." << std::endl;
45+
}
46+
return 0;
47+
}

0 commit comments

Comments
 (0)