answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
frozen
2 months ago
7

JAVAThe method longestStreak is intended to determine the longest substring of consecutive identical characters in the parameter

str and print the result.For example, the call longestStreak("CCAAAAATTT!") should print the result "A 5" because the longest substring of consecutive identical characters is "AAAAA".
Complete the method below. Your implementation should conform to the example above.
public static void longestStreak(String str)
Computers and Technology
1 answer:
Harlamova29_29 [1K]2 months ago
5 0

Answer:

public static void longestStreak(String str){

int maxCharacterCount = 0;

char longestSubString = str.charAt(0);

for(int i=0; i<str.length(); i++){

int currentMaxCharacterCount = 1;

for(int j=i+1; j<str.length(); j++){

if(str.charAt(i) != str.charAt(j)){

break;

}

else {

currentMaxCharacterCount++;

}

}

if (currentMaxCharacterCount > maxCharacterCount) {

maxCharacterCount = currentMaxCharacterCount;

longestSubString = str.charAt(i);

}

}

System.out.println(longestSubString + " "+ maxCharacterCount);

}



Explanation:

Let’s examine the code from start to finish.

- Two variables are initialized: maxCharacterCount to keep track of the longest character sequence in the string, and longestSubString which holds the longest substring found so far. They start at 0 and the first character of the string, respectively.

- The purpose is to check for all substrings to find the longest one. Hence, a nested for loop is employed (a loop within another loop).

- The outer loop commences at the first character of the input string, extending to its end.

- Within this outer loop, currentMaxCharacterCount counts the currently observed maximum character repetitions.

- The inner loop kicks off from the next character and verifies if it matches the character currently being examined in the outer loop.

- The loop halts if there’s a difference.

- If they match, currentMaxCharacterCount increments by 1, suggesting a possible new longest substring

* Note: When i equals 0, j grows from 1 up to the length of the string. When i is 1, j goes from 2 to the string length, and this cycle continues for all i and j.

- If currentMaxCharacterCount surpasses maxCharacterCount, it indicates a new longest substring, and maxCharacterCount ought to reflect this new count.

- To proceed to look for the subsequent longest substring, it carries on with the next iteration of i (longestSubString = str.charAt(i);)

- Ultimately, once both loops are finished, longestSubString and maxCharacterCount should be displayed.

You might be interested in
The president of the company wants a list of all orders ever taken. He wants to see the customer name, the last name of the empl
zubka84 [1067]

Response:

refer to the explanation

Clarification:

Examine the SQL statement shown below:

SELECT c.CustomerName, e.LastName, s.ShipperName, p.ProductName, o.Quantity, od.OrderDate

FROM

Customers c, Employees e, Shippers s, Orders o, OrderDetails od, Products p

WHERE c.customerID = o.customerID AND

e.employeeID = o.employeeID AND

o.orderID = od.orderID AND

od.shipperID = s.shipperID AND

od.productID = p.productID;

6 0
1 month ago
What feature would be used to collect how many times users downloaded a product catalog?
ivann1987 [1066]
Event Tracking is an important capability within Google Analytics used to capture user interactions with various website elements.
8 0
1 month ago
Discussion Question 10: A bank in California has 13 branches spread throughout northern California , each with its own minicompu
Harlamova29_29 [1022]
The banking system that poses greater risk of vulnerabilities is the one with ten branches dispersed across California, where data resides on a central mainframe located in San Francisco. If the branches do not share data across the network, the risk of hacking is reduced. However, with a network setup, both data sharing and centralized storage increase exposure to unauthorized access.
3 0
1 month ago
Represent decimal number 8620 in (a) BCD, (b) excess-3 code, (c)2421 code, and (d) as a binary number
maria [1035]

The decimal number 8620 can be expressed in the following ways:

a = 1000 0110 0010 0000

b = 1011 1001 0101 0011

c = 1110 1100 0010 0000

d = 10000110101100

I hope these representations answer your question and prove useful.

4 0
2 months ago
List at least six things you would check for if you were asked to evaluate the workspace of an employee for ergonomics
maria [1035]

Evaluating the risks related to employees remaining stationary for extended periods at their workstations is critical. Below is a compilation of key ergonomic principles that aid in identifying ergonomic risk factors.


<span>1.    </span>Are employees kept in a neutral posture?

<span>2.    </span>Does the workstation provide opportunities for movement and stretching?

<span>3.    </span>Is the lighting sufficient?

<span>4.    </span>Are the chairs adjustable as needed?

<span>5.    </span>Are suitable footrests available?

<span>6.    </span>Is there additional storage to enhance desk organization?






0 0
2 months ago
Other questions:
  • Two middle-order batsmen are compared based on their performance in their previous cricket match.
    7·1 answer
  • What are the differences between a policy, a standard, and a practice? What are the three types of security policies? Where woul
    15·1 answer
  • Users report that the network access is slow. After questioning the employees, the network administrator learned that one employ
    7·1 answer
  • More Loops: All versions of foods.py in this section have avoided using for loops when printing to save space. Choose a version
    13·1 answer
  • A developer writes a trigger on the Account object on the before update event that increments a count field. A workflow rule als
    12·1 answer
  • Factoring of integers. Write a python program that asks the user for an integer and then prints out all its factors. For example
    13·1 answer
  • The principal that users have access to only network resources when an administrator explicitly grants them is called __________
    6·1 answer
  • The dealer's cost of a car is 85% of the listed price. The dealer would accept any offer that is at least $500 over the dealer's
    7·1 answer
  • Write a class for a Cat that is a subclass of Pet. In addition to a name and owner, a cat will have a breed and will say "meow"
    9·1 answer
  • Write a MATLAB function named lin_spaced_vector with two inputs and one return value. The first input will be a single real numb
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!