utkubulkan
BAN USERC++ solution with example string
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isOpen(char c)
{
return c == '(' || c == '{' || c == '[';
}
bool isClose(char c)
{
return c == ')' || c == '}' || c == ']';
}
bool isMatch(char c1, char c2)
{
return (((c1 == '(') && (c2 == ')')) ||
((c1 == '[') && (c2 == ']')) ||
((c1 == '{') && (c2 == '}')) );
}
bool isBalanced(const string& str)
{
stack<char> charStack;
for (unsigned int i = 0; i < str.size(); ++i ) {
if (isOpen(str[i])) {
charStack.push(str[i]);
} else if (isClose(str[i])) {
if (!charStack.empty() && isMatch(charStack.top(), str[i])) {
charStack.pop();
} else {
return 0;
}
}
}
return charStack.empty();
}
int main()
{
string str = "([[(){}]])({})";
cout << str << " : " << isBalanced(str) << endl;
}
- utkubulkan February 03, 2016c++ answer with random tree fill.
- utkubulkan February 03, 2016#include <iostream>
using namespace std;
class tree {
public:
tree *left;
tree *right;
int data;
tree(tree *l,tree *r, int d) : left(l), right(r), data(d) { }
};
int findMaxDepth(tree *node)
{
if (node == 0) {
return 0;
}
return (max(findMaxDepth(node->left), findMaxDepth(node->right))) + 1;
}
int main() {
// your code goes here
tree *root, *buffy, *dummy;
root = buffy = new tree(0,0,1);
for (int i = 2; i < 100; i++) {
dummy = new tree(0,0,i);
if (rand() % 2) {
buffy->left = dummy;
buffy = buffy->left;
} else {
buffy->right = dummy;
buffy = buffy->right;
}
}
cout << "depth " << findMaxDepth(root);
return 0;
}
- utkubulkan February 03, 2016
Repjuliaaperez05, Cloud Support Associate at ABC TECH SUPPORT
I Performed extensive web research to collect pertinent data and gather images related to the assigned articleIts act of writing ...
Repmarierlewis2, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am Marie and I live in Los Angeles USA, I have a strong base in marketing and I believe ...
C++ Solution
}
- utkubulkan February 03, 2016