1answer.
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]
3 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]3 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
What are important acronyms we use when we talk about the internet? this is for computer science.
vovangra [49]

Answer:

Lol

Brb

Btw

Gtg

Explanation:

I hope this helps

7 0
3 years ago
What can you add to your presentation from the insert ribbon toolba?
stiks02 [169]

Answer:

anything you want

Explanation:

there are a lot of adons for powerpoint

7 0
3 years ago
In order to increase typing speed Minisoft has redesigned its keyboard and provided a chime that is related to typing speed. The
mash [69]

Answer:

Dependent variable: speed of typing.  

Independent variables: keyboard design & chime.

Explanation:

The speed of typing is expected to be dependent on the keyboard and chime, which are independent variables controlled by the experiment.

6 0
3 years ago
Which situation is an example of? "relational context" in the transactional model of? communication?
Jlenok [28]
An example I believe of relational context is when I was out with my son on the weekend (he has a developmental disability) and we had agree I would buy him a 1/2 sub sandwich the day before but then he said I would like a McFlurry so I said okay and then he said so you mean I can have a McFlurry and a sub and I thought oh oh I stuck my foot in it so I said but it must be only a 1/2 sub so he said no I want a full sub then and no mcflurry so I agreed so from the original 1/2 sub idea the idea evolved to a full sub which was affected by the warm sunny summery weather in the afternoon and seeing people lined up at ice cream shops so the idea developed in relation to the weather, how hungry he was and the social aspect people buying ice cream.
8 0
3 years ago
What if an html code became cracked and formed a bug how can i repair that
Valentin [98]
I suggest you look up a video for it ofc
7 0
3 years ago
Other questions:
  • Which one of these is a mem?
    8·1 answer
  • To resolve a domain name on the internet, the dns resolver first contacts:
    5·1 answer
  • In addition to meeting OSHA standards, the Department of Defense requires each military department, including the Department of
    12·1 answer
  • What are the four basic operating principles of the information processing cycle?
    7·2 answers
  • The US government takes the protection of patients' private information very seriously. In 1996, went into effect. This law prot
    14·1 answer
  • Deon plans to ride a 15 mile bicycle trail.his if his average is 20 miles per hour
    15·1 answer
  • Write a program that include a method that returns the sum of all the elements of a Linked List of Integers. Allow the user to e
    11·1 answer
  • Which of the following statements about version control are true? Select 3 options.
    7·2 answers
  • How will you define a text?
    10·1 answer
  • Sean is frustrated with having to use separate editors, compilers, libraries, plug-ins, and debugers. What could Sean use to mak
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!