diff --git a/Yash2110-Yash/BalancedBrackets(HR)/BalancedBrackets.cpp b/Yash2110-Yash/BalancedBrackets(HR)/BalancedBrackets.cpp new file mode 100644 index 0000000..6daff74 --- /dev/null +++ b/Yash2110-Yash/BalancedBrackets(HR)/BalancedBrackets.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +string ltrim(const string &); +string rtrim(const string &); + +/* + * Complete the 'isBalanced' function below. + * + * The function is expected to return a STRING. + * The function accepts STRING s as parameter. + */ + +string isBalanced(string s) +{ + stack st; + int len = s.size(); + int i; + + for(i=0;i(isspace))) + ); + + return s; +} + +string rtrim(const string &str) { + string s(str); + + s.erase( + find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), + s.end() + ); + + return s; +} diff --git a/Yash2110-Yash/BalancedBrackets(HR)/README.md b/Yash2110-Yash/BalancedBrackets(HR)/README.md new file mode 100644 index 0000000..0abf0aa --- /dev/null +++ b/Yash2110-Yash/BalancedBrackets(HR)/README.md @@ -0,0 +1,11 @@ +Question: +
+ Link: https://www.hackerrank.com/challenges/balanced-brackets/problem?h_r=internal-search +
+Answer: +
+ - Here we use stacks to first store the opening brackets and then compare the last added bracket with the closing brakcets being added. +
+ - We keep poping out the brackets from the stack which are balanced i,e they find their opposite pair in sequence +
+ - If in the end we are able to make the stack empty then the string has balanced brackets \ No newline at end of file