We are given a string S of length N consisting only of letters 'A' and/or 'B'....
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
We are given a string S of length N consisting only of letters 'A' and/or 'B'. Our goal is to obtain a string in the format "A...AB...B" (all letters 'A' occur before all letters 'B') by deleting some letters from S. In particular, strings consisting only of letters 'A' or only of letters 'B' fit this format. Write a function: class Solution { public int solution (String S); } that, given a string S, returns the minimum number of letters that need to be deleted from S in order to obtain a string in the above format. Examples: 1. Given S = "BAAABAB", the function should return 2. We can obtain "AAABB" by deleting the first occurrence of 'B' and the last occurrence of 'A'. 2. Given S = "BBABAA", the function should return 3. We can delete all occurrences of 'A' or all occurrences of 'B'. 3. Given S = "AABBBB", the function should return O. We do not have to delete any letters, because the given string is already in the expected format. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000]; string S is made only of the characters 'A' and/or 'B'. We are given a string S of length N consisting only of letters 'A' and/or 'B'. Our goal is to obtain a string in the format "A...AB...B" (all letters 'A' occur before all letters 'B') by deleting some letters from S. In particular, strings consisting only of letters 'A' or only of letters 'B' fit this format. Write a function: class Solution { public int solution (String S); } that, given a string S, returns the minimum number of letters that need to be deleted from S in order to obtain a string in the above format. Examples: 1. Given S = "BAAABAB", the function should return 2. We can obtain "AAABB" by deleting the first occurrence of 'B' and the last occurrence of 'A'. 2. Given S = "BBABAA", the function should return 3. We can delete all occurrences of 'A' or all occurrences of 'B'. 3. Given S = "AABBBB", the function should return O. We do not have to delete any letters, because the given string is already in the expected format. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000]; string S is made only of the characters 'A' and/or 'B'.
Expert Answer:
Answer rating: 100% (QA)
The problem described in the image involves processing a string containing only the letters A andor B to reformat it so that all As occur before any B... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Helen Strand's regular hourly wage rate is $14, and she receives an hourly rate of $21 for work in excess of 40 hours. During a January pay period, Helen works 47 hours. Helen's federal income tax...
-
A substring of a string is a contiguous sequence of characters from the string. For example, BC is a substring of ABCD which starts from the second character of ABCD. Another example, ABC is a...
-
Marc attended University of Wisconsin for two years and earned his Bachelor of Science degree. Assume that Mother lives 40 years and dies with an estate of $100,000. Her only heirs are her three...
-
Lucy has just been promoted to a managerial position and given a new office. She is very fond of small Persian carpets and Native American paintings and wants to get some carpets and paintings for...
-
The comprehensive regional hospital discussed in the section "Resource Planning for Service Providers" performs bypass surgery at its state-of-the-art facility. Similar to aneurysm repair, this...
-
1. Discuss the effects of grinding and turning on the endurance limit of steel. What is the reason of change in fatigue strength with various surfaces finishes? 2. List all the available machining...
-
Financial statements must contain objective and verifiable data if they are to be useful. Yet, many estimates and subjective assumptions are required to facilitate the preparation of these reports....
-
Sycamore Plastics (SP) is a manufacturer of polyethylene plastic pellets used as a raw material by manufacturers of plastic goods around the U. S. SP currently operates four manufacturing centers in...
-
Explore the challenges and opportunities associated with outsourcing certain aspects of business operations.
-
Use the data for the demand for chicken given in Exercise 7.19. Suppose you are told that the true demand function is ln Y t = 1 + 2 ln X 2t + 3 ln X 3t + 6 ln X 6t + u t (1) but you think...
-
From case study - Patagonia 1.Explain the checklist ingredients that you would value most as an employee? Which would you value least? Do some suggestions seem debatable? 2. Discuss the checklist...
-
A recent study shows that 18% of adults in the US do not use the Internet. Suppose that 12 adults are randomly selected in the US. a)Construct the PMF of X, the number of adults who don't use...
-
To remodel your business, you need to sign a 190-day note with proceeds of $16,559.57 and a maturity value of $20,000. Find the APR.
-
The debits to Work in Process-Assembly Department for April, together with data concerning production, are as follows: April 1, work in process: Materials cost, 3,000 units $8,065 Conversion costs,...
-
1.HDV Company manufactures video filming equipment.HDV's most popular product, the HDV52 sells for $1,400 per unit.Costs per unit associated with the HDV52 are as follows: Direct materials $ 420 /...
-
5. Let f: [-1,1] R be a function which satisfies |f(x)| x for every x = [-1, 1]. Show that f is differentiable at 0 and find f'(0).
-
Why should companies should exercise holistic strategic planning towards supply chain risk management? What benefits come from that? Why companies should Invest in safe production mechanisms,...
-
Charles owns an office building and land that are used in his trade or business. The office building and land were acquired in 1978 for $800,000 and $100,000, respectively. During the current year,...
-
Define the optimization problem LONGEST-PATH-LENGTH as the relation that associates each instance of an undirected graph and two vertices with the number of edges in a longest simple path between the...
-
Given a set of m linear inequalities on n variables x 1, x 2, . . . ,x n , the linearin equality feasibility problem asks whether there is a setting of the variables that simultaneously satisfies...
-
Observe that, using the structures in this section, the way we find the successor and predecessor of a value x does not depend on whether x is in the set at the time. Show how to find the successor...
-
Define flowcharting.
-
Flowchart symbols represent what aspect of a system?
-
List and draw the basic flowchart symbols.
Study smarter with the SolutionInn App