Dsa
Two Pointers
Medium
Balanced Parentheses Subsequence
You are given a string containing only characters '(', ')', and other letters. Your task is to find the length of the longest subsequence that contains balanced parentheses. The other letters can be ignored.
Input: A single line containing the string. The length of the string will be between 1 and 100,000.
Output: Print a single integer representing the length of the longest balanced parentheses subsequence.
Example:
Input:
ab(c(d)e)f(g)h
Output:
6
Key concepts
two_pointersstringsstack
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now