Sequence in java

To restart from scratch :
What I understand your question should have been :

I have a sequence of Strings and want to check the difference between them. Whenever a line is the same as the previous one, i want to increment a counter (that counted the occurence of that line). Whenever a new line appears, i want to increment another counter and reset the first one to 1.
I don’t expect the line be equal to a several times older one.
I want the response in the form of the following String : #numberOfTimesTheLineChanged(on 5 digits)#numberOfTimeTheLineAppeared#

For example :
When inputed

aaa
aaa
aab
aab
aab
bab

I expect :

#00001#1#
#00001#2#
#00002#1#
#00002#2#
#00002#3#
#00003#1#

My code is :

Blabla.

And I would then have answered you :

You can try :

public static List<String> INPUT = Arrays.asList("aaa","aaa","aab","aab","aab","bab");  // my input data
public static List<String> OUTPUT = Arrays.asList("#00001#1#","#00001#2#","#00002#1#","#00002#2#","#00002#3#","#00003#1#"); //my expected response

public static String RESPONSE_FORMAT = "#%05d#%01d#"; //formatting the response into a String;

public static void main(String[] args) {
    List<String> output = new ArrayList<>();
    int counterOccurence = 0;
    int counterChanges = 0;
    String lastLine = "";
    for(String line : INPUT){
        if(lastLine.equals(line)){
            counterOccurence++;
        } else {
            counterOccurence= 1;
            counterChanges++;
        }
        output.add(String.format(RESPONSE_FORMAT, counterChanges,counterOccurence));
        lastLine = line; //remember last line
    }
    System.out.println(output);
    System.out.println("It matches : "+output.equals(OUTPUT));

It returns me with :

[#00001#1#, #00001#2#, #00002#1#, #00002#2#, #00002#3#, #00003#1#]
It matches : true

Does that answer your question ?

Leave a Comment