Skip to content

Commit

Permalink
Merge pull request #3563 from SujayShanbhag/new-algorithm
Browse files Browse the repository at this point in the history
feat: Added balanced parenthesis expression validation algorithm
  • Loading branch information
ZoranPandovski authored Oct 20, 2023
2 parents fbbfad7 + 9bff2cf commit 62a1a54
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions strings/C++/Valid_parenthesis_expression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <string>
#include <stack>
using namespace std;

class Solution {
public:
bool checkValidString(string s) {
int n = s.length();
stack<int> open, star;
for (int i = 0; i < n; i++) {
if (s[i] == '(') open.push(i); // keeping track of where open brackets are.
else if (s[i] == '*') star.push(i); // and stars
else { // s[i]==')'
if (!open.empty()) open.pop(); // if open exists
else if (!star.empty()) star.pop(); // if star exists
else return false; // if none then invalid
}
}
// stack not empty meaning open parenthesis not closed
while (!open.empty()) {
if (star.empty()) return false; // no way to validate
else {
if (open.top() > star.top()) return false; // if star happened before open bracket
else {
// star can be used as close
star.pop();
open.pop();
}
}
}
return true;
}
};

int main() {
Solution solution;
string input = "(*))";
bool isValid = solution.checkValidString(input);
if (isValid) {
cout << "The string is valid." << endl;
} else {
cout << "The string is not valid." << endl;
}
return 0;
}

0 comments on commit 62a1a54

Please sign in to comment.