Write a java program to find the length of the largest chunk in the string

Write a java program to find the length of the largest chunk in the string.  Read a string and return the length of the largest “chunk” in the string.
A chunk is a repetition of the same character 2 or more number of times. If the given string does not contain any repeated chunk of characters return -1.

The return type is the integer.

Input and Output Format:
Input consists of a string.
The output consists of the integer.
Refer sample output for formatting specifications.

Sample Input 1:
This place is soooo good

Sample Output 1:
4

java program to find the length of the largest chunk in the string

import java.util.*;
public class Main {
public static void main(String[] args) {
String s1="this is soooo good";
System.out.println(maxChunk(s1));
}
public static int maxChunk(String s1) {
int max=0;
StringTokenizer t=new StringTokenizer(s1," ");
while(t.hasMoreTokens()){
String s2=t.nextToken();
int n=0;
for(int i=0;i<s2.length()-1;i++)
if(s2.charAt(i)==s2.charAt(i+1))
n++;
if(n>max)
max=n;
}
return (max+1);
}
}

 

Leave a Reply

Your email address will not be published.

17 − 4 =