stumped on a Java interview, need some hints

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

    Your use of LinkedList is interesting (and potentially unexpected), but some of the other aspects are a bit strange & distracting ...

    My first instinct would have been to repeatedly loop over the String, replacing characters into a StringBuilder - with a while loop surrounding the foor loop (as suggested by sehe). This may be what the interviewer was expecting, rather than your clever use of LinkedList.

    The interviewer may be distracted by these other aspects. e.g.:

    1. why not use a LinkedList instead of LinkedList.
    2. why not return a LinkedList directly from deconstruct(String). No need to wrap it.
    3. You don't really need a reconstruct() method. Just use temp.size()
    4. Regex is a bit of an undesirable way to get the third char. I can't think of a one-liner, but you could use an array, like so:

      private static Character getThirdChar(Character firstChar, Character secondChar) {
          List li= new ArrayList(Arrays.asList('a', 'b', 'c'));
          li.remove(firstChar);
          li.remove(secondChar); 
          return li.get(0);
      }
      

    After these edits, the interviewer might be able to focus more clearly on your very interesting solution.

    EDIT: perhaps the question is asking you to return the smallest string itself, not its length. I think last line of the interview question ought to read as follows: "For the given string, return the smallest string which can result by applying this operation repeatedly"

    HTH

提交回复
热议问题