mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 06:24:07 +00:00
day 30: 678-valid-parenthesis-string
This commit is contained in:
parent
34446fb0a9
commit
69ba49b561
10
678-valid-parenthesis-string/driver.cpp
Normal file
10
678-valid-parenthesis-string/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
string s = "((**)((()))";
|
||||
bool answer = true;
|
||||
Solution soln;
|
||||
cout << "Validated parenthesis string correctly? " << soln.test(s, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
33
678-valid-parenthesis-string/soln.cpp
Normal file
33
678-valid-parenthesis-string/soln.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
bool Solution::checkValidString(string s)
|
||||
{
|
||||
int min_open = 0, max_open = 0;
|
||||
for (int i = 0; i < s.length(); i++)
|
||||
{
|
||||
if (s[i] == '(')
|
||||
{
|
||||
min_open++;
|
||||
max_open++;
|
||||
}
|
||||
else if (s[i] == ')')
|
||||
{
|
||||
min_open--;
|
||||
max_open--;
|
||||
}
|
||||
else
|
||||
{
|
||||
min_open--;
|
||||
max_open++;
|
||||
}
|
||||
if (max_open < 0)
|
||||
break;
|
||||
min_open = max(min_open, 0);
|
||||
}
|
||||
return min_open == 0;
|
||||
}
|
||||
|
||||
bool Solution::test(string s, bool answer)
|
||||
{
|
||||
return checkValidString(s) == answer;
|
||||
}
|
||||
8
678-valid-parenthesis-string/soln.hpp
Normal file
8
678-valid-parenthesis-string/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
bool checkValidString(string s);
|
||||
bool test(string s, bool answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user