import java.util.*;
public class Solution {
    public static List<List<Integer>> mergeIntervals(List<List<Integer>> intervals) {

        List<List<Integer>> outerList = new ArrayList<>();

        List<Object> innerList = new ArrayList<>();  //this is the list inside list to satisfy return type

        int counter=0;

        String odd="";
        String even="";

        int endFirstRange;
        int startSecondRange;

        String evenSubStringStartInterval="";
        String oddSubStringEndInteveral="";

        String oddSubStringStartInterval="";
        String evenSubStringEndInterval="";

        //first need to understand that return type is List < List<Integer> >
        //I am not sure if System.out.println() will print this since I am typically not used to having a collection within a collection..
        //Since we are returning variable of this datatype, I presume the test environment will take care of this scenario..
        // It will only become an issue once I configure the driver class with main method

        //Also require an iterator through the list

        Iterator<List<Integer>> it = intervals.iterator();

        //since the data inside mergeIntervals is a List, I think the hasNext() might not be applicable
        //since this would refer to a String....

        while (it.hasNext())
        {
            //This will also have a list item
            Object temp = it.next();
            //String temp1 = (String)(temp);

            //int a = Integer.valueOf(temp1);
            System.out.println(temp);

            //this will print out the existing intervals....
            //need a method now to change the object into a String so that it can be examined
            



            //now we know the overlap can occur only on the closing interval (for a range) vs start interval (next range) of the next number
            //but need to control the flow of this loop also since we can only make comparisons when
            //two ranges appear

            //instead of using the example, I want to adjust my focus to triple overlap, and also subsequent standalone. I believe this will put in better position against all test cases.

            //{1,3}, {2,8}, {6,10}, {11,14}

            //There are also two approaches:
            //I can either let it store all the ranges into a String array and then perform the merging of overlapping functions.
            //OR
            //handle it in real time (this is preferred)

            counter++;

           if (counter%2==1)
           {

            odd = String.valueOf(temp);
            oddSubStringEndInteveral=odd.substring((odd.indexOf(" ")+1),odd.indexOf("]"));
            
            oddSubStringStartInterval = odd.substring(1,odd.indexOf(","));

            System.out.println("RANGE1 start:" + oddSubStringStartInterval);
            System.out.println("RANGE1 end:" + oddSubStringEndInteveral);
            

           }

            //we have two ranges to compare
            if (counter%2==0)
            {
                even = String.valueOf(temp);

                evenSubStringStartInterval = even.substring(1,even.indexOf(","));
                evenSubStringEndInterval = even.substring((even.indexOf(" ")+1),even.indexOf("]"));

                System.out.println("RANGE2 start:" + evenSubStringStartInterval);
                System.out.println("RANGE2 end:" + evenSubStringEndInterval);


            } 
            
            //These are the intervals in the range that need to be compared
            
        

            if (odd!="" && even!="")
            {
                if (Integer.valueOf(oddSubStringEndInteveral)>Integer.valueOf(evenSubStringStartInterval))
                {
                    //overlap
                   // {1,3}, {2,8}, {6,10}, {11,14}

                    //we need to also set the Strings to blank at the end

                    //so they can both populate again

                    odd="";
                    even="";

                    //we know the next iteration will be odd {6,10}
                    //but this has to be compared against  even {2,8}

                    System.out.println("HERE");

                }

                else
                {
                    System.out.println("HERE1");

                    //only required to add odd since the next range (even) might overlap with next odd
                    
                     //ODD
                    //{1,3}, {2,8}, {6,10}, {11,14}

                    Object dd = odd;

                    innerList.add(dd);


                    outerList.add(innerList);

                    System.out.println("HERE2");

                   

                    //We know the next counter will be three
                    //and store   odd = {6,10}

                    //in even there will be  even = {2,8} 

                    //so there is no interest in wiping state of Strings odd or even

                    //however in the first comparison, we completed an odd element in the list
                    //and compared against even....
                    //now are retrieving an odd and comparing against an even.
                    //unsure if this has any impact anywhere...

                }
            }


            //int endFirstRange = Integer.valueOf(odd.substring((odd.indexOf(",")+1),odd.indexOf("]")));

            //There appears to be a [ ]  around the test cases....
            //so need to start at 1 and not 0 
            //int endSecondRange = Integer.valueOf(even.substring(1,even.indexOf(",")));




        }

         return outerList;
    }

}