Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:06:10+00:00 2026-06-13T19:06:10+00:00

Given question: A string of parentheses is said to be balanced if the left-

  • 0

Given question:

A string of parentheses is said to be
balanced if the left- and right-parentheses in the string can be paired off properly. For example, the strings "(())" and "()()" are both balanced, while the string "(()(" is not
balanced.
Given a string S of length n consisting of parentheses, suppose you want to find the longest subsequence of S that is balanced. Using dynamic programming, design an algorithm that finds the longest balanced subsequence of S in O(n^3) time.

My approach:
Suppose given string: S[1 2 … n]
A valid sub-sequence can end at S[i] iff S[i] == ‘)’ i.e. S[i] is a closing brace and there exists at least one unused opening brace previous to S[i]. which could be implemented in O(N).

#include<iostream>
using namespace std;
int main(){
    string s;
    cin >> s;
    int n = s.length(), o_count = 0, len = 0;
    for(int i=0; i<n; ++i){
        if(s[i] == '('){
            ++o_count;
            continue;
        }
        else if(s[i] == ')' && o_count > 0){
            ++len;
            --o_count;
        }
    }
    cout << len << endl;
    return 0;
}

I tried a couple of test cases and they seem to be working fine. Am I missing something here? If not, then how can I also design an O(n^3) Dynamic Programming solution for this problem?

This is the definition of subsequence that I’m using.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:06:11+00:00Added an answer on June 13, 2026 at 7:06 pm

    For O(n^3) DP this should work I think:

    dp[i, j] = longest balanced subsequence in [i .. j]
    dp[i, i] = 0
    dp[i, i + 1] = 2 if [i, i + 1] == "()", 0 otherwise
    
    dp[i, j] = max{dp[i, k] + dp[k + 1, j] : j > i + 1} in general
    

    This can be implemented similar to how optimal matrix chain multiplication is.

    Your algorithm also seems correct to me, see for example this problem:

    http://xorswap.com/questions/107-implement-a-function-to-balance-parentheses-in-a-string-using-the-minimum-nu

    Where the solutions are basically the same as yours.

    You are only ignoring the extra brackets, so I don’t see why it wouldn’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question: Given a SQL string like CREATE VIEW TestView AS SELECT value1, value2 FROM
I have recently come across an interesting question on strings. Suppose you are given
My main question is given a feature centroid, how can I draw it in
I came across this question: say given two weights 1 and 3, u can
Can you help me out with this question please. Question: Given the following array
In the question how to check if the url contains a given string. Javascript/jquery
Fast RegExp question. I must check whether a given string contains aaa but not
I saw this in an interview question , Given a sorting order string, you
This is an interview question: Given a string, find all its permutations that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.