You have inconsistent spacing, generally if you have 4 spaces for indentation, stick to it for the rest of the code, nothing is saved by not including it, and it makes it easier for other people to read.
The same idea for names, shortening them doesn't really give much other than saving a few bytes, and makes it much much harder to understand what goes on in a few months, so descriptive names are better, unless the abbr. is really obvious or defined
Below is your code with better spacing, and suggestions for better variable names, although the suggestions are not perfect, try and improve on them
public class SeqSameChar {
public static void main(String[] args) {
int longestSequenceLength = 0;
Scanner scan = new Scanner(System.in);
String[] input = null;
input = scan.nextLine().split(" ");
String[] characters = new String[input.length];
for(int i = 0; i < characters.length; i++) {
characters[i] = String.valueOf(input[i]);
}
longestSequenceLength = lengthOfLongestSequence(characters,input.length);
System.out.println(longestSequenceLength);
for(int i = index; i < index + longestSequenceLength; i++) {
System.out.print(characters[i]);
}
}
public static int index;
//to calculate the longest contiguous increasing sequence
public static int lengthOfLongestSequence(String[] characters, int size){
if(size <= 0) return 0;
int longestFound = 1;
int current = 1;
for(int i = 1; i < size; i++){
if(characters[i].equals(characters[i-1])){
current++;
}
else{
if(current > longestFound){
index = i - current;
longestFound = current;
}
current = 1;
}
}
return longestFound;
}
}
Next lets look at smaller parts
int longestSequenceLength = 0;
Why is this so far away from where it is used? Try to keep the declaration of variables to as near to their first use as possible
String[] input = null;
input = scan.nextLine().split(" ");
Good, but you don't need the null here
String[] input = scan.nextLine().split(" ");
String[] characters = new String[input.length];
for(int i = 0; i < characters.length; i++) {
characters[i] = String.valueOf(input[i]);
}
This piece of code doesn't actually achieve anything, you are basically copying a String array to another String array, using an unnecessary parse too.
Since it is not needed, you can just delete it, and change any later references in the code to it, to input instead.
lengthOfLongestSequence(characters,input.length);
This could have lead to a bug, since you are passing an array, and passing the length of another array. If someone ever changed either array between copying them and the call of this method, it would break.
There is no real need to pass the length of an array anyway, in Java it is easy enough to get, and isn't a costly operation
for(int i = index; i < index + longestSequenceLength; i++) {
System.out.print(characters[i]);
}
Since it is going to be the same character, why do we need to loop over the array? We could just print the character once and it would serve the same purpose
public static int index;
Huh? I didn't notice the global variable for quite a while, it looks like it is a method. I would move this to the top, above main.
It would not be good practice, but if I was being lazy, I would just print the character before returning the length of the longest sequence, or return an array or list with both in it, rather than using a global. I just prefer not to use global variables until I have to.
if(size <= 0) return 0;
If a negative size was passed in, it is really a cause for concern, so we should throw an exception here. If we don't pass in the size, there is no need to check for it, so we can just replace this with
int size = characters.length;
And everything works out
for(int i = 1; i < size; i++){
if(characters[i].equals(characters[i-1])){
current++;
}
else{
if(current > longestFound){
index = i - current;
longestFound = current;
}
current = 1;
}
}
There is a bug here, if you reach the end of the array, and the longest sequence is at the end, what happens? I'll leave it to you to figure out the fix.
A hint towards what I would do is try to add an extra check at the end
Spoiler below for code with all the changes, it is not the best, but hopefully it helps
public class SeqSameChar {
public static int index;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] input = scan.nextLine().split(" ");
int longestSequenceLength = lengthOfLongestSequence(input);
System.out.println(longestSequenceLength);
System.out.println(input[index]);
}
//to calculate the longest contiguous increasing sequence
public static int lengthOfLongestSequence(String[] characters){
int size = characters.length;
int longestFound = 1;
int current = 1;
for(int i = 1; i <= size; i++){
if(i < size && characters[i].equals(characters[i-1])){
current++;
}
else{
if(current > longestFound){
index = i - current;
longestFound = current;
}
current = 1;
}
}
return longestFound;
}
}
bddfDDDffkl
at first, which didn't work as expected. Entering the string asb d d f D D D f f k l
seemed to work though. Please make it clear how you expect the input. \$\endgroup\$