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 8143693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:11:11+00:00 2026-06-06T13:11:11+00:00

I have seen various discussions and code attempts at solving the "String reduction" problem

  • 0

I have seen various discussions and code attempts at solving the "String reduction" problem from interviewstreet.com, but none of them does it via dynamic programming.

Listed under the Dynamic Programming section, the problem is described as follows:

Given a string consisting of a,b and c’s, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if ‘a’ and ‘c’ are adjacent, they can replaced with ‘b’.

What is the smallest string which can result by applying this operation repeatedly?

The problem can be solved using exhaustive brute-force search, effectively creating a tree of all possible substitutions:

// this is more or less pseudo code from my head
int GetMinLength(string s)
{
    // solve edge cases
    if (s.Length == 1) return 1;
    if (s.Length == 2) return ReduceIfPossible(s);

    // recursive brute force
    int min = s.Length;
    for (int i = 0; i<s.Length-1; i++)
    {
        if (s[i] != s[i+1])
        {
            var next = GetMinLength(
                  s.Substring(0, i) + 
                  Reduce(s[i], s[i + 1]) +
                  s.Substring(i + 2)
                  );

            if (next < min) min = next;
        }
    }
}

This obviously fails for larger N (N <= 100), so I am trying to break it into smaller subproblems, memoize them, and merge results.

The problem is that I cannot determine the state which would have "optimal substructure", needed to apply dynamic programming (or in other words to "merge" results from sub-problems). Minimizing a part of the string doesn’t guarantee that the final string will really be the smallest solution.

What would be the subproblem "state" in this case, which could be merged towards the final solution?

  • 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-06T13:11:13+00:00Added an answer on June 6, 2026 at 1:11 pm

    What makes this tricky is that you need to treat this as 2 dynamic programming problems in a row.

    1. Build up a table of, by character you wind up with, by start position, all of the possible end positions that are a block that can be reduced to that character.
    2. The smallest length that the final i characters of the string can be reduced to. (The table that you built in step 1 can be used to recursively reduce this problem to already solved subproblems.)

    The second provides your answer. It is much more straightforward if you have already solved the first.

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

Sidebar

Related Questions

I have seen various discussions on the problem of serving WebView pages from assets,
I have seen various code snippets which find outs co-ordinates from various methods like
I have seen various answers around helping explain adding records in one-to-many relationships but
I have seen this asked in various places but never seen a workable solution
Throughout various code, I have seen memory allocation in debug builds with NULL ...
I have seen various libraries in PHP for the Authorize.net Customer Information Manager, but
I searched and searched google and various forums for the problem and have seen
Quite some time i`ve learnt MVC. I have seen various techniques to post data
I have seen config files(yes, text files) for various console applications that look like
I have seen on various websites how developers version their css/javascripts files by specifying

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.