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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:49:26+00:00 2026-05-27T20:49:26+00:00

This is an interview problem that I am stuck on: Given a string consisting

  • 0

This is an interview problem that I am stuck on:

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?

My attempted solution:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    public static void main(String[] args) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    System.in));

            System.out.println(solve(in.readLine()));

            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int solve(String testCase) {
        LinkedList<String> temp = new LinkedList<String>(deconstruct(testCase));

        for (int i = 0; i < (temp.size() - 1); i++) {
            if (!temp.get(i).equals(temp.get(i + 1))) {
                temp.add(i, getThirdChar(temp.remove(), temp.remove()));
                i = -1;
            }
        }

        return reconstruct(temp).length();
    }

    private static List<String> deconstruct(String testCase) {
        List<String> temp = new LinkedList<String>();

        for (int i = 0; i < testCase.length(); i++) {
            temp.add(testCase.charAt(i) + "");
        }

        return temp;
    }

    private static String reconstruct(List<String> temp) {
        String testCase = "";

        for (int i = 0; i < temp.size(); i++) {
            testCase += temp.get(i);
        }

        return testCase;
    }

    private static String getThirdChar(String firstChar, String secondChar) {
        return "abc".replaceAll("[" + firstChar + secondChar + "]+", "");
    }
}

The code seems to work fine on test inputs “cab” (prints “2”), “bcab” (prints “1”), and “ccccc” (prints “5”). But I keep getting told that my code is wrong. Can anyone help me figure out where the bug is?

  • 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-05-27T20:49:27+00:00Added an answer on May 27, 2026 at 8:49 pm

    As people have already pointed out the error is that your algorithm makes the substitutions in a predefined order. Your algorithm would make the transformation:

    abcc --> ccc
    instead of
    abcc --> aac --> ab --> c

    If you want to use the technique of generating the reduced strings, you need to either:

    • Perform substitutions on one level in all the orders imaginable (instead of only one predefined iteration order)
    • Find a smart way of deciding which substitution will yield the shortest string in the end

    If all you need is the length of the reduced string, there is however a much simpler
    implementation which does not require the reduced strings to be generated. This is an extended version of @Matteo’s answer, with some more details and a working (very simplistic) algorithm.

    Simple properties

    I postulate that the following three properties are true about abc-strings under the given set of rules.

    1. If it is impossible to reduce a string further, all the characters in that string must be the same character.

    2. It is impossible that: 2 < answer < string.length is true

    3. While performing a reduction operation, if the counts of each letter prior to the operation is even, the count of each letter after the operation will be odd. Conversely, if the counts of each letter is odd prior to the operation, the counts will be even after the operation.

    Property 1

    Property one is trivial.

    Property 2 (example to illustrate)

    Assume: we have a reduced string of length 5 which can be reduced no more.

    AAAAA

    As this string is the result of a reduction operation, the previous string must’ve contained one B and one C. Following are some examples of possible “parent strings”:

    BCAAAA, AABCAA, AAACBA

    For all of the possible parent strings we can easily see that at least one of the C:s and the B:s can be combined with A:s instead of each other. This will result in a string of length 5 which will be further reducible. Hence, we have illustrated that the only reason for which we had an irreducible string of length 5 was that we had made incorrect choice of which characters to combine while performing the reduction operation.

    This reasoning applies for all reduced strings of any length k such that 2 < k < string.length.

    Property 3 (example to illustrate)

    If we have for example [numA, numB, numC] = [even, even, even] and perform a reduction operation in which we substitute AB with a C. The count of A and B will decrease by one, making the counts odd, while the count of C will increase by one, making that count odd as well.

    Similarly to this, if two counts are even and one is odd, two counts will be odd and one even after the operation and vice versa.

    In other words, if all three counts have the same “evenness”, no reduction operation can change that. And, if there are differences in the “evenness” of the counts, no reduction operation can change that.

    Drawing conclusions which result from the properties

    Consider the two irreducible strings:

    A and AA

    For A notice that [numA, numB, numC] = [odd, even, even]
    For AA notice that [numA, numB, numC] = [even, even, even]

    Now forget those two strings and assume we are given an input string of length n.

    If all characters in the string are equal, the answer is obviously string.length.

    Else, we know from property 2 that it is possible to reduce the string to a length smaller than 3. We also know the effect on evenness of performing reduction operations. If the input string contains even counts of all letters or odd count of all letters, it is impossible to reduce it to a single letter string, since it is impossible to change the evenness structure from [even, even, even] to [odd, even, even] by performing reduction operation.

    Hence a simpler algorithm would be as follows:

    Algorithm

    Count the number of occurences of each letter in the input string [numA, numB, numC]
    
    If two of these counts are 0, then return string.length
    
    Else if (all counts are even) or (all counts are odd), then return 2
    
    Else, then return 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this problem from an interview with Microsoft. Given an array of random
Recently in a job interview, I was given the following problem. Say I have
I was given this problem in an interview. How would you have answered? Design
I came across this problem during an interview forum., Given an int array which
Can anyone please help me with the Algorithm for this problem (its an Interview
Someone I know went to an interview and was given the following problem to
I came across this problem while preparing for an interview and curious to know
This was an interview question. Given Visual Studio 2008 and an icon saved as
This is an interview question I faced recently. Given an array of 1 and
This was an job placement interview I faced. They asked whether we can realloc

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.