Write a function: def solution (S) that, given a string S of letters "L" and "R", denoting the types of shoes in line (left or right), returns the maximum number of intervals such that each interval contains an equal number of left and right shoes. For example, given S "RLRRLLRLRRLL", the function should return 4, because S can be split into intervals: "RL, "RRLL", "RL" and "RRLL". Note that the intervals do not have to be of the same size.

Respuesta :

Answer:

Please look at attachment. Thanks

Ver imagen Jerryojabo1

Answer:

# The function solution is defined with String S as argument

def solution(S):

   # number of letter R is assigned to letterRcount

   # it is initialized to 0

   letterRcount = 0

   # number of letter L is assigned to letterLcount

   # it is initialized to 0

   letterLcount = 0

   # the total count of 'RL' is assigned to total_count

   total_count = S.count("RL")

   # for loop that goes through the string

   for letter in S:

       # if the letter is R, it continue

       if(letter == 'R'):

           continue

       # once it continue, it will increment letterRcount

           letterRcount += 1

       # else if the letter is L, it will increment letterLcount

       elif (letter == 'L'):

           letterLcount += 1

   # if letterRcount is equals to letterLcount, total_count will be incremented

   if(letterRcount == letterLcount):

       total_count += 1

   # the value of total_count is returned

   return total_count

   

# the solution function is called with a string arguments

print(solution("RLRRLLRLRRLL"))

Explanation:

The program is written in python and well commented. A sample of program output is attached.

Ver imagen ibnahmadbello