-
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
7 changed files
with
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ | |
"typeinfo": "cpp", | ||
"variant": "cpp", | ||
"format": "cpp", | ||
"fstream": "cpp" | ||
"fstream": "cpp", | ||
"stack": "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
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,17 @@ | ||
project(valid_parentheses) | ||
|
||
add_executable( | ||
valid_parentheses_runner | ||
runner.cpp | ||
) | ||
|
||
add_executable( | ||
valid_parentheses_tests | ||
tests.cpp | ||
) | ||
target_link_libraries( | ||
valid_parentheses_tests | ||
GTest::gtest_main | ||
) | ||
|
||
gtest_discover_tests(valid_parentheses_tests) |
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,3 @@ | ||
# 20. Valid Parentheses | ||
|
||
[LeetCode link](https://leetcode.com/problems/valid-parentheses/) |
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,13 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "solution.hpp" | ||
|
||
int main() | ||
{ | ||
const std::string input("[]"); | ||
Solution s; | ||
std::cout << "\"" << input << "\": " << (s.isValid(input) ? "true" : "false") << std::endl; | ||
|
||
return 1; | ||
} |
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,31 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <string> | ||
|
||
class Solution { | ||
public: | ||
bool isValid(const std::string& s) { | ||
std::stack<char> st; | ||
for (char c : s) | ||
{ | ||
if (c == '(' || c == '[' || c == '{') | ||
st.push(c); | ||
else | ||
{ | ||
if (st.empty()) | ||
return false; | ||
char curr = st.top(); | ||
st.pop(); | ||
if (c == ')' && curr != '(') | ||
return false; | ||
else if (c == ']' && curr != '[') | ||
return false; | ||
else if (c == '}' && curr != '{') | ||
return false; | ||
} | ||
} | ||
|
||
return st.empty(); | ||
} | ||
}; |
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,38 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "solution.hpp" | ||
|
||
class ValidParenthesesTest :public ::testing::TestWithParam<std::pair<std::string, bool>> { | ||
protected: | ||
Solution solution; | ||
}; | ||
|
||
TEST_P(ValidParenthesesTest, CheckSolution) { | ||
std::string input = std::get<0>(GetParam()); | ||
bool expected = std::get<1>(GetParam()); | ||
|
||
bool actual = solution.isValid(input); | ||
|
||
EXPECT_EQ(expected, actual); | ||
} | ||
|
||
INSTANTIATE_TEST_SUITE_P( | ||
ValidParenthesesTests, | ||
ValidParenthesesTest, | ||
::testing::Values( | ||
std::make_pair<std::string, bool>("{}", true), | ||
std::make_pair<std::string, bool>("[]", true), | ||
std::make_pair<std::string, bool>("()", true), | ||
std::make_pair<std::string, bool>("{}{}[]", true), | ||
std::make_pair<std::string, bool>("{{}}", true), | ||
std::make_pair<std::string, bool>("{[]}", true), | ||
std::make_pair<std::string, bool>("{", false), | ||
std::make_pair<std::string, bool>("}", false), | ||
std::make_pair<std::string, bool>(")()", false), | ||
std::make_pair<std::string, bool>("(}", false), | ||
std::make_pair<std::string, bool>("({)", false) | ||
) | ||
); |