Answer to Question #263875 in C++ for python code

Question #263875

Write a C++ program to check for the balancing symbols in C++, the symbols can be /* */, [],


{}, and ().

1
Expert's answer
2021-11-10T11:27:53-0500
bool correct_paranthesis(string str){
	stack<char> stk;
	map<char,char> bracket_map;
	bracket_map[')'] = '(';
	bracket_map['}'] = '{';
	bracket_map[']'] = '[';
	
	for(int i=0; i<str.size(); i++){
		if(str[i] == '(' || str[i] == '{' || str[i] == '[')
			stk.push(str[i]);
		if(str[i] == ')' || str[i] == '}' || str[i] == ']'){
			if(stk.empty())
				return false;
			if(stk.top() == bracket_map[str[i]] )
				stk.pop();
		}
	}
	
	return (stk.empty() == true);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS