MelitoMT's blog

By MelitoMT, history, 22 months ago, In English

We are given two cases in which we can obtain regular bracket sequences (RBS) being "()()" , "(())", we can summarize this with an observation. As it is guaranteed that we can always arrange the brackets in a way we obtain a RBS, we only have to worry with the cases in which we have the closing bracket before the opening bracket, cases like ")(", note that if we count the occurrences of opening and closing brackets whenever we have one of this cases we will have that the count of closing brackets is bigger that the count of opening brackets. Let's use an example, given the input ")))((((())" we start counting, just before the first opening bracket, we will already have the closing brackets count in 3, that is, we will need 3 movements so far. Being said this, we will need a count of the difference of closing brackets and opening brackets, and whenever it gets to a maximum value we have a new number of least movements to obtain a RBS (the variable max in the code below).

  • Vote: I like it
  • +8
  • Vote: I do not like it