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

  • Home
  • SEARCH
  • 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 8287657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:06:38+00:00 2026-06-08T12:06:38+00:00

I am solving this problem- Given a string consisting of a,b and c’s, we

  • 0

I am solving this problem-

Given a string consisting of a,b and c’s, we can 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?

Now I have written the following recursive solution (far from efficient), but want to convert it to either top-down or bottom-up solution.

Problem: I am not able to come up with a tabular structure for memoization. Even though I have to output only the length of resulting string, how can I solve it without actually solving the problem. The strings are getting reduced, so how do I store them?

Any hint for DP solution or Memoization would be great!

EDIT Many people have come up with top-down memoization solution, please try bottom-up as well.

#include <iostream>
#include <string>

using namespace std;

string reduce(string s)
{
    if (s.length() <= 1)
        return s;

    int k;
    char c = s[0];
    string min = s;
    for (k = 1; k < s.length() && c; ++k)
        if (s[k] != c)
            c = 0;
    if (c)
        return s;

    if (s.length() == 2){
        if (s[0] != 'a' && s[1] != 'a')
            s[0] = 'a';
        else if (s[0] != 'b' && s[1] != 'b')
            s[0] = 'b';
        else if (s[0] != 'c' && s[1] != 'c')
            s[0] = 'c';
        s.resize(1);
        return s;
    }

    for (k = 1; k < s.length(); ++k){
        string s1 = reduce(s.substr(0, k));
        string s2 = reduce(s.substr(k));

        if (s1.length() + s2.length() < min.length())
            min = s1 + s2;
        if (!s1.empty() && !s2.empty() && s1.back() != s2.front()){
            if (s1.back() != 'a' && s2.front() != 'a')
                s1.back() = 'a';
            else if (s1.back() != 'b' && s2.front() != 'b')
                s1.back() = 'b';
            else if (s1.back() != 'c' && s2.front() != 'c')
                s1.back() = 'c';
            s1 = reduce(s1 + s2.substr(1));
            if (s1.length() < min.length())
                min = s1;
        }

    }
    return min;
}

int main()
{
    string input;
    cin >> input;
    cout << reduce(input) << endl;
    return 0;
}
  • 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-08T12:06:39+00:00Added an answer on June 8, 2026 at 12:06 pm

    I’m a bit too lazy to think the problem through, but I’ll give you an approach to memoization that often enough works.

    Instead of recursing directly, introduce mutual recursion.

    std::string reduce(std::string const &s)
    {
        // ...
        string s1 = reduce_memo(s.substr(0, k));
        string s2 = reduce_memo(s.substr(k));
        // ...
    }
    

    where reduce_memo maintains a hash table, i.e. an unordered_map, mapping subproblems to their solutions.

    // static is incredibly ugly, but I'll use it here for simplicity
    static std::unordered_map<std::string, std::string> memo;
    
    std::string reduce_memo(std::string const &s)
    {
        try {
            return memo.at(s);
        } except (std::out_of_range const &) {
            std::string r = reduce(s);
            memo[s] = r;
            return r;
        }
    }
    

    When programming in C++98, use std::map instead of unordered_map.

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

Sidebar

Related Questions

I've been problem solving this for the past few hours and frustratingly can't find
Here is the problem: Remove specified characters from a given string. Input: The string
I'm having trouble solving this problem: Create a function that given a character set
What´s the most efficient, elegant and pythonic way of solving this problem? Given a
Here's the description of this problem: You are given two integers a and b.
I need some help in solving this problem. We have a large amount of
I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
This question is more about guidance than actually solving my problem: I need to
I'm solving this problem: G(n) is defined as G(n) = G(n-1) + f(4n-1) ,

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.