stumped on a Java interview, need some hints

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

提交回复
热议问题