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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:54:38+00:00 2026-06-04T13:54:38+00:00

I am preparing myself for an interview that I have on monday and I

  • 0

I am preparing myself for an interview that I have on monday and I found this problem to resolve called “String Reduction“. The problem is stated like this :

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?

For instance, cab -> cc or cab -> bb, resulting in a string of length
2. For this one, one optimal solution is: bcab -> aab -> ac -> b. No more operations can be applied and the resultant string has length 1.
If the string is = CCCCC, no operations can be performed and so the
answer is 5.

I have seen a lot questions and answers on stackoverflow but I would like to verify my own algorithm. Here is my algorithm in pseudo code. In my code

  1. S is my string to reduce
  2. S[i] is the character at index i
  3. P is a stack:
  4. redux is the function that reduces the characters.

    function reduction(S[1..n]){        
    P = create_empty_stack();
    for i = 1 to n
    do
       car = S[i];
       while (notEmpty(P))
       do
          head = peek(p);
          if( head == car) break;
          else {
             popped = pop(P);
             car = redux (car, popped);
           }  
       done
       push(car)
    done
    return size(P)}
    

The worst-case of my algorithms is O(n) because all the operations on the stack P is on O(1). I tried this algorithm in the examples above, I get the expected answers.
Let me execute my algo with this example ” abacbcaa” :

i = 1 :
   car = S[i] = a, P = {∅}
   P is empty, P = P U {car} -> P = {a}

 i = 2 :
   car = S[i] = b, P = {a}
   P is not empty :
       head = a
       head != car ->
            popped = Pop(P) = a 
            car = reduction (car, popped) = reduction (a,b) = c
            P = {∅}

    push(car, P) -> P = {c}



i = 3 :
   car = S[i] = a, P = {c}
   P is not empty :
       head = c
       head != car ->
            popped = Pop(P) = c 
            car = reduction (car, popped) = reduction (a,c) = b
            P = {∅}

    push(car, P) -> P = {b}


 ...


 i = 5 : (interesting case)
  car = S[i] = c, P = {c}
   P is not empty :
       head = c
       head == car -> break

    push(car, P) -> P = {c, c}


 i = 6 :
  car = S[i] = b, P = {c, c}
   P is not empty :
       head = c
       head != car ->
            popped = Pop(P) = c 
            car = reduction (car, popped) = reduction (b,c) = a
            P = {c}

   P is not empty : // (note in this case car = a)
       head = c
       head != car ->
            popped = Pop(P) = c 
            car = reduction (car, popped) = reduction (a,c) = b
            P = {∅}
    push(car, P) -> P = {b}

... and it continues until n

I have run this algorithm on various examples like this, it seems to work.
I have written a code in Java that test this algorithm, when I submit my code to the system, I am getting wrong answers. I have posted the java code on gisthub so you can see it.

Can someone tell me what is wrong with my algorithm.

  • 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-04T13:54:40+00:00Added an answer on June 4, 2026 at 1:54 pm

    I am going to try to explain what nhahtdh means. There are multiple reasons why your algorithm fails. But the most fundamental one is that at each point in time, only the first character observed has a chance to be pushed on the stack p. It should not be this way, as you can start a reduction basically from any position.

    Let me give you the string abcc. If I breakpoint at

    car = S[i];
    

    The algo run as :

    p = {∅}, s = _abcc //underscore is the position
    p = {a}, s = a_bcc  
    p = {c}, s = ab_cc  
    

    At this point you are stuck with a reduction ccc

    But there is another reduction : abcc -> aac ->ab ->c

    Besides, returning the size of the stack P is wrong. cc cannot be reduced, but
    the algorithm will return 1. You should also count the number of times you skip.

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

Sidebar

Related Questions

I am preparing for my interview and came across this question: Consider that i
Pros and cons and Usage I have been preparing myself for an interview for
Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses();
I am preparing for my interview tomorrow -- I need the answer to this
Just preparing myself for a task that i feel could be quite troublesome, and
I'm preparing my paypal system and have a separate page that forwards the user
I'm not great with MySQL, so I often find myself preparing sub-optimal queries that
I am preparing myself for programming competitions and i would like to know how
I am preparing for a technical interview and I am stuck at writing this
I am preparing to start on a new short-term contract (1-2 months) that involves

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.