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
-
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...
-
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...
-
Mirabeau Mining paid $582,000 for the right to extract mineral assets from a 400,000-ton mineral deposit. In addition to the purchase price, Mirabeau Mining also paid a $1,600 filing fee; a $4,400...
-
Billy and his first cousin, Sally, were raised together as children. They have the same values, go to the same school, have the same friends and are extremely happy together. One day, Billy asks...
-
On April 23, 2014, Calvin Loyer admitted his wife, Edeltrud Loyer, to a nursing home administered by Signature Healthcare. During the admissions process, Calvin signed an arbitration agreement...
-
Union Express has 60 tons of cargo that needs to be shipped from Boston to Dallas. The shipping capacity on each of the routes Union Express planes fly each night is shown in the following table:...
-
Should court fines for offenses (i.e. traffic, quality of life, shoplifting, property damage, etc.) be the same for the poor and the wealthy? Should it be dollar for dollar equal, or should it be a...
-
Fred and Lillian are adults with developmental disabilities. They are married and live in a public housing unit. They wanted to have children, but Lillian discovered that she had been sterilized when...
-
Pseudolistening is: O Taking innocent comments as personal attacks. Avoid topics that are uncomfortable. Listening to only what interests you. O An imitation of listening.
-
Question 6 The use of the first person voice in a literary work is a clear indication of the author's personal opinion on an issue or event True False
-
Question 3 The "closest clich syndrome" is embodied in the idea that a reader has summarized closely the view the author actually expresses in a literary work. True 10 pts False
-
Given a hash function h ( ( n ) = n mod 5, what would a computers memory cells look like if we were to input values 2 , 1 0 , and 1 4 ?
-
Several techniques are available to reduce the taxable income of a C corporation and thus avoid double taxation. Those techniques include all of the following except: O Increasing distributions...
-
What is the potential energy of a + 6 nC load located 50 mm from a + 80 C load? What is the potential energy if the same load is 50 mm from a -80 C load?
-
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...
-
Find the input-output differential equation relating \(v_{o}\) and \(v_{i}(t)\) for the circuit shown below. w R R Vo v(t) R L
-
Consider the following circuit where \(i_{i}(t)\) is the input current and \(i_{o}(t)\) is the output current. (a) Obtain the State-Variable Matrix model \((A, B, C, D)\) for the circuit. (b) Obtain...
-
(a) For the following circuit find the State-Variable Matrix model (A, B, C, D) where \(v_{o}\) is the output voltage and \(v_{i}\) is the input voltage. (b) Also, find the input-output differential...
Study smarter with the SolutionInn App