Skip to content

Commit

Permalink
Solved valid parentheses.
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Jun 9, 2023
1 parent 53afd52 commit ad65ebc
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"typeinfo": "cpp",
"variant": "cpp",
"format": "cpp",
"fstream": "cpp"
"fstream": "cpp",
"stack": "cpp"
}
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ add_subdirectory( longest-substring-without-repeating-characters )
add_subdirectory( second-minimum-node-in-a-binary-tree )
add_subdirectory( top-k-frequent-elements )
add_subdirectory( two-sum )
add_subdirectory( valid-parentheses )
17 changes: 17 additions & 0 deletions valid-parentheses/CMakeLists.txt
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)
3 changes: 3 additions & 0 deletions valid-parentheses/README.md
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/)
13 changes: 13 additions & 0 deletions valid-parentheses/runner.cpp
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;
}
31 changes: 31 additions & 0 deletions valid-parentheses/solution.hpp
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();
}
};
38 changes: 38 additions & 0 deletions valid-parentheses/tests.cpp
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)
)
);

0 comments on commit ad65ebc

Please sign in to comment.