stumped on a Java interview, need some hints

后端 未结 8 1903
天涯浪人
天涯浪人 2021-01-30 18:00

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

8条回答
  •  一个人的身影
    2021-01-30 18:41

    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
    

提交回复
热议问题