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
JulijaS [17]
2 years 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:
Butoxors [25]2 years 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 start from the top of the code and go all the way down.

- Two variables created; <em>maxCharacterCount</em> to count the longest character in the string, and <em>longestSubString</em> to save the current longest substring. Initially they are set to 0 and the first character of given string respectively.

- We need to check all the substrings to find longest one. That is why a <u>nested for loop</u> (for loop inside a for loop) is created.

- The first loop starts from the first character of the given string and goes until the end of the given string.

- Inside the first loop, <em>currentMaxCharacterCount</em> is created to hold the current value of the maximum character in the given string.

- The second loop starts from the second character of the string, and checks if the character is same as the one that is being checked by the first loop.

- If they are not same, then the loop stops.

- If they are the same characters, <em>currentMaxCharacterCount</em> is incremented by 1. (That means we may find our new longest substring)

* Reminder: When the <em>i</em> is equal to 0, <em>j </em>is increasing from 1 to given string length. When <em>i</em> is equal to 1 <em>j</em> is increasing from 2 to given string length and so on. This process repeats for all <em>i</em> and <em>j</em> values to find out the longest substring.

- If <em>currentMaxCharacterCount </em>is greater than<em> maxCharacterCount, </em>that means we have a new longest substring, and <em>maxCharacterCount </em>should be equal to this new substring's count.

- Then, to be able to check for the next possible longest substring, it should continue from the next iteration of the <em>i </em>(longestSubString = str.charAt(i);)

- Finally, when the loops are done checking, <em>longestSubString</em> and <em>maxCharacterCount</em> need to be printed.

You might be interested in
Which Internet of Things (IoT) challenge involves the difficulty of developing and implementing protocols that allow devices to
dolphi86 [110]

Answer:

Option C i.e., Interoperability is the correct option

Explanation:

Interoperability performs for computers or its components for the purpose to communicate and it is important to improve the development of the Internet of Things. It performs the communication as well as share their services between computer or its components.

It also contains challenges occurred at the time of developing and implementing the protocols by which they communicate.

7 0
2 years ago
Chinh wants to have a program print, "Sorry, but that isn’t one of your options" until the user enters the correct information.
kolbaska11 [484]

Answer:

a wile loop

Explanation:

8 0
2 years ago
Matt is a senior developer for Cyber Protect, a company that helps secure management information systems. Matt's new task is to
Len [333]

Answer:

white-hat hacker

Explanation:

The white-hat hacker is also called as ethical hacking. The white hat hacker is the security of the computer in an ethical manner by some experts it specializing in system testing and other test modules of computer is to ensure the security of the information security of an organization.

As Matt is a  developer for protecting the Cyber in a company The Matt creates secure management information systems. it divides the new task into the computer So Matt is a white-hat hacker in that organization

4 0
2 years ago
An attacker distributes hostile content on Internet-accessible Web sites that exploit unpatched and improperly secured client so
julia-pushkina [17]

Answer:

Since the attack impacts the client SANS Critical Security Controls. This act affects the Inventory of authorized and unauthorized software running on the victim's machine.

5 0
2 years ago
Read 2 more answers
Consider the following 3-PARTITION problem. Given integers a1; : : : ; an, we want to determine whether it is possible to partit
ZanzabumX [31]

Answer:

Explanation:

Find attach the solution

6 0
2 years ago
Other questions:
  • In an ethernet network, the signal that is sent to indicate a signal collision is called a ________ signal.
    7·1 answer
  • Which feature of Badoo helped members make their profile more prominent?
    14·1 answer
  • vertical exchanges are typically used only to buy and sell materials required for an organization's support activities ( True or
    14·2 answers
  • Given an alphabet, print all the alphabets up to and including the given alphabet.
    15·2 answers
  • Earlier in the day, you created a user account for Brenda Cassini (bcassini). When she tries to log in, she can't. You realize t
    9·2 answers
  • Which option in the Caption dialog box configures whether the caption appears above or below the image
    11·2 answers
  • The properly marked source document states: (C) Operation Panda will take place on 29 September. The new document states: (C) On
    8·1 answer
  • Write a program that takes a single integer input from the user and stores it in a variable. Your program should increase the va
    15·1 answer
  • Finish the program to compute how many gallons of paint are needed to cover the given square feet of walls. Assume 1 gallon can
    9·1 answer
  • In this problem, you will derive the efficiency of a CSMA/CD-like multiple access protocol. In this protocol, time is slotted an
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!