-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_valid_parenthesis_string test.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <string> | ||
|
||
// Time: O(N) | ||
// Space: O(1) | ||
|
||
namespace stack { | ||
|
||
// Time: O(N) | ||
// Space: O(N) | ||
class Solution { | ||
public: | ||
static bool checkValidString(std::string s) { | ||
std::stack<int> openBrackets, asterisks; | ||
for (int i = 0; i < std::ssize(s); ++i) { | ||
if (s[i] == '(') { | ||
openBrackets.push(i); | ||
} else if (s[i] == '*') { | ||
asterisks.push(i); | ||
} else if (s[i] == ')') { | ||
if (!openBrackets.empty()) { | ||
openBrackets.pop(); | ||
} else if (!asterisks.empty()) { | ||
asterisks.pop(); | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
throw; | ||
} | ||
} | ||
|
||
while (!openBrackets.empty() && !asterisks.empty()) { | ||
if (asterisks.top() < openBrackets.top()) { | ||
return false; | ||
} | ||
openBrackets.pop(); | ||
asterisks.pop(); | ||
} | ||
return openBrackets.empty(); | ||
} | ||
}; | ||
|
||
} // namespace stack | ||
|
||
namespace opt { | ||
|
||
// Time: O(N) | ||
// Space: O(1) | ||
class Solution { | ||
public: | ||
static bool checkValidString(std::string s) { | ||
int open = 0, close = 0; | ||
for (auto c : s) { | ||
if (c == '(') { | ||
++open, ++close; | ||
} else if (c == ')') { | ||
--open, --close; | ||
} else if (c == '*') { | ||
--open, ++close; | ||
} else { | ||
throw; | ||
} | ||
open = std::max(0, open); | ||
if (close < 0) { | ||
return false; | ||
} | ||
} | ||
return open == 0; | ||
} | ||
}; | ||
|
||
} // namespace opt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::string s; | ||
bool expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.s = "()", | ||
.expected = true, | ||
}, | ||
{ | ||
.s = "(*)", | ||
.expected = true, | ||
}, | ||
{ | ||
.s = "(*))", | ||
.expected = true, | ||
}, | ||
}; | ||
|
||
SECTION("STACK") { | ||
for (const auto &[s, expected] : test_cases) { | ||
const auto actual = stack::Solution::checkValidString(s); | ||
REQUIRE(expected == actual); | ||
} | ||
} | ||
|
||
SECTION("MEMORY OPTIMIZED") { | ||
for (const auto &[s, expected] : test_cases) { | ||
const auto actual = opt::Solution::checkValidString(s); | ||
REQUIRE(expected == actual); | ||
} | ||
} | ||
} |