day 30: 678-valid-parenthesis-string

This commit is contained in:
Kaushik Narayan R 2023-10-31 16:30:58 -07:00
parent 34446fb0a9
commit 69ba49b561
3 changed files with 51 additions and 0 deletions

View 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;
}

View 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;
}

View 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);
};