 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Length of longest balanced parentheses prefix using Java
In this article we will explore how we can find the length of longest balanced parentheses prefix using java , first we will understand the problem with some examples then we learn two different approaches to find it.
Problem Statement
Here, we will be given a string with parentheses and we need to find the length of balanced parentheses set from the string i.e. for every open parentheses "(" if there is a close parentheses ")", then we call it balanced.
Prefix defines the first balanced set from the string, let the parenthesis set '(())()' we consider only '(())'
Input Output Scenarios
Let us see some input-output scenarios, for a better understanding.
- Suppose the input string: is "(()" here the balanced parentheses prefix is (), then length is 2.
- If the input string is "((()()))(((" here the balanced parentheses prefix is ((()())) then length is 8.
- and if the input string is "(()())()()" the balanced parentheses prefix is (()()) then length is 6.
We can get the length of the longest balanced parentheses prefix
- Using the Stack data structure
- Counting open and close Parentheses
Using the Stack Data Structure
We can use a stack, then from the stack, if we get an open parentheses '(' push it to the stack, and if we get a close parentheses then pop the stack and increment a counter variable by 2 (a balanced pairs length is 2), continuing this when we get empty stack we we will return the counter variable.
Algorithm
Following is the algorithm
Step 1: Initialize a stack and a counter.
Step 2: Iterate through each character in the string.
- If character is ( push it onto the stack.
- If character is ) pop the stack.
- Increment counter by 2
- Check whether the stack is empty or not
- If empty, break;
Step 3: Finally return the counter;
Example
 import java.util.Stack; public class Example { public static int longestBalancedPrefix(String s) { Stack<Character> stack = new Stack<>(); int count = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '(') { stack.push(ch); } else if (ch == ')') { if (!stack.isEmpty()) { stack.pop(); count += 2; if (stack.isEmpty()) { break; } } } } return count; } public static void main(String args[]) { String s = "((())(("; System.out.println("input string is: " + s); System.out.println("Length of longest balanced parentheses prefix is " + longestBalancedPrefix(s)); } }   Output
input string is: ((())(( Length of longest balanced parentheses prefix is 4
Counting Open & Close Parentheses
In this approach we will take two variable let one is count and another is length . Then from the string if the character is "(" we will increment the count by one and if that character is ")" we decrement the count by one and increment the length by two and check whether the count is zero or not, if zero then, break the loop and return length.
Example
 public class Example { public static void main(String args[]) { String s = "((())())(())"; int count = 0; int length = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '(') { count++; } else if (ch == ')') { count--; length = length + 2; if (count == 0) { break; } } } System.out.println("Input string is " + s); System.out.println("Length of the longest balanced parentheses prefix is " + length); } }  Output
Input string is ((())())(()) Length of the longest balanced parentheses prefix is 8
