stumped on a Java interview, need some hints

后端 未结 8 1880
天涯浪人
天涯浪人 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:51

    Edit For fun I did my own version that operates on a char[] in-place:

    public class Main {
    
        public static void main(String[] args) {
            System.out.println(solve("abbccaacba"));
        }
    
        private static int solve(String testCase) {
            if (!testCase.matches("^[abc]*$"))
                throw new IllegalArgumentException("invalid input");
    
            char[] chars = new char[testCase.length()];
            testCase.getChars(0, testCase.length(), chars, 0);
    
            int remaining = chars.length;
    
            for (int i=0; (i<chars.length) && (remaining>1);)
            {
                int next = i+1;
                while (next < chars.length && (' '==chars[next]))
                    ++next;
    
                if (next >= chars.length)
                    break;
    
                if (chars[i]!=chars[next])
                {
                    switch (chars[i])
                    {
                        case 'a': chars[next] = ('b'==chars[next])? 'c':'b'; break;
                        case 'b': chars[next] = ('a'==chars[next])? 'c':'a'; break;
                        case 'c': chars[next] = ('b'==chars[next])? 'a':'b'; break;
                    }
                    chars[i] = ' '; // mark as removed
                    remaining--;
    
                    while (i>0 && (' '!=chars[i-1]))
                        --i;
                    if (' '==chars[i])
                        i = next;
                }
                else
                    ++i;
            }
    
            return remaining;
        }
    }
    

    See it live on http://ideone.com/yhK9t, with debug output:

    a<bbccaacba
     c<bccaacba
      a<ccaacba
       b<caacba
        a<aacba
        aa<acba
        aaa<cba
        a<a bba
        aa< bba
        a<  cba
           b<ba
           bb<a
           b< c
             a<
             a Done.
    1
    

    Still do note caveats I mentioned in my comments: EDIT Huh, somehow I borked a comment saying that the answers would vary depending on the order of substitutions

    • left to right or right-to-left (my version uses left-to-right)
    • depth-first (or breadth-first) (my version uses depth-first)
    0 讨论(0)
  • 2021-01-30 18:57

    In my opinion, the answer is a number (or a program that generates a number) and not a program that applies the described transformation.

    For this reason, (if the string is not empty) the answer would be 1 with several inputs.

    However if the input is composed of a single character repeated several times, the string cannot be elaborated, hence the output string would be the same as the input string (i.e., same length).

    Note that the input string must be composed of a single character; if it has two characters, the output would be 1: baaaa -> caaa -> baa -> ca -> b

    Note that the sequence of replacements has not been specified, (if more than 1 replacements is available). Hence we cannot say a lot more, but we can observe that some strings not composed of a single characters cannot be reduced to a string of length 1. This is the case when all the three letters appear in sequence (e.g., abc). When this string is processed, the output would be a string of two equal characters (e.g., cc or aa) which cannot be reduced even more.

    0 讨论(0)
提交回复
热议问题