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.