Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A challenge question created in our Java class for the first person to finish that we weren't offered the solution to because our teacher planned

image text in transcribed

A challenge question created in our Java class for the first person to finish that we weren't offered the solution to because our teacher planned to use it for upcoming years

Because Bessie is bored of playing with her usual text string where the only characters are 'C,' 'O,' and 'W,' Farmer John gave her Q new strings (1Q100), where the only characters are 'M' and 'O.' Bessie's favorite word out of the characters 'M' and 'O' is obviously "MOO," so she wants to turn each of the Q strings into "MOO" using the following operations: 1. Replace either the first or last character with its opposite (so that ' M ' becomes ' O ' and 'O' becomes 'M'). 2. Delete either the first or last character. Unfortunately, Bessie is lazy and does not want to perform more operations than absolutely necessary. For each string, please help her determine the minimum number of operations necessary to form "MOO" or output 1 if this is impossible. INPUT FORMAT (input arrives from the terminal/ stdin): The first line of input contains the value of Q. The next Q lines of input each consist of a string, each of its characters either 'M' or 'O'. Each string has at least 1 and at most 100 characters. OUTPUT FORMAT (print output to the terminal/ stdout): Output the answer for each input string on a separate line. SAMPLE INPUT: 3 MOMMOM MMO MOO SAMPLE OUTPUT: 4 1 A sequence of 4 operations transforming the first string into "MOO" is as follows: Replace the last character with 0 (operation 1) Delete the first character (operation 2) Delete the first character (operation 2) Delete the first character (operation 2) The second string cannot be transformed into "MOO." The third string is already "MOO," so no operations need to be performed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions

Question

Write an elaborate note on marketing environment.

Answered: 1 week ago