Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Balanced Parentheses Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Balanced Parentheses

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string with just the six characters - ‘(’, ‘)’, ‘{’, ‘}', ‘[’ and ‘]’. Determine if the string is balanced.

Approach

The approach for this problem is pretty simple. 

  • Declare an empty stack.
  • Traverse the string and whenever an opening bracket is encountered, insert it into the stack.
  • When a closing bracket is encountered, check if the last bracket in the stack matches the current closing bracket. If not then return false otherwise remove the last bracket from the stack.
  • After traversing the string, if the stack is empty return true otherwise return false.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
bool isBalancedParentheses(string str) {
    int n = str.size();
	stack<char> openingBrackets;
	for (int i = 0; i < n; i++) {
		if (str[i] == '(' || str[i] == '{' || str[i] == '[') {
			openingBrackets.push(str[i]);
		} else {
			if (openingBrackets.empty()) {
				return false;
			}
			if (str[i] == ')') {
				char last = openingBrackets.top();
				openingBrackets.pop();
				if (last != '(') {
					return false;
				}
			}
			if (str[i] == '}') {
				char last = openingBrackets.top();
				openingBrackets.pop();
				if (last != '{') {
					return false;
				}
			}
			if (str[i] == ']') {
				char last = openingBrackets.top();
				openingBrackets.pop();
				if (last != '[') {
					return false;
				}
			}
		}
	}
	return openingBrackets.empty();
}
Java
class Solution {
	boolean isBalancedParentheses(String str) {
	    int n = str.length();
		Stack<Character> openingBrackets = new Stack<>();
		for (int i = 0; i < n; i++) {
			if (str.charAt(i) == '(' || str.charAt(i) == '{' || str.charAt(i) == '[') {
				openingBrackets.push(str.charAt(i));
			} else {
				if (openingBrackets.empty()) {
					return false;
				}
				if (str.charAt(i) == ')') {
					char last = openingBrackets.peek();
					openingBrackets.pop();
					if (last != '(') {
						return false;
					}
				}
				if (str.charAt(i) == '}') {
					char last = openingBrackets.peek();
					openingBrackets.pop();
					if (last != '{') {
						return false;
					}
				}
				if (str.charAt(i) == ']') {
					char last = openingBrackets.peek();
					openingBrackets.pop();
					if (last != '[') {
						return false;
					}
				}
			}
		}
		return openingBrackets.isEmpty();
	}
}
Related Content
Evaluate Reverse Polish Notation
Implement Queue using Stacks
Implement Stack using Array
Implement Stack using Linked List
Implement Stack using Queues
Largest Rectangle in Histogram
Implement Min Stack
Next Greater Element
Trapping Rain Water
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet