/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

//With the smallest window size, the problem arises because if we are comparing String s against String p
//the benefit of removing characters from both StringBuilders doesn't seem as logical as anagram.
//This is because we are heavily relying on the indexes in this challenge and if we find a match against Stringbuilder temp (String s)
//and perform a character deletion, it will disrupt the indexing.
//if we query sb (String p) against String s, the danger is of course that duplicates (String p) will use the same index in String s if a match occurs.
//This leaves me with the option of changing the main for loop with temp (containing substring of String s)
//If it finds a character match, I take index (it is absolute to String s if I add the startPos).
//If it finds another character match, this is where I need to use a bit of improvisation.
// We know that indexes will reduce of all elements to the right of the deletion.
//So if the first match occurs at index 6 on temp
//And second match occurs on index 6 again, I add one to the index (to ensure it is absolute to String s)
//Indexes 5 and below I do not touch..
//if the third match occurs lets say index 6, now we add two since every element to right has moved two spaces to the left

public class Main 
{
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //TEST CASE 1:
        findAnagrams("acbacebabacdabc", "abcc");
        
        //TEST CASE 2:
        //findAnagrams("abab", "ab");
        
        //TEST CASE 3:
        //findAnagrams("azazazazazazzzzaaazazazaz", "az");
        
        //TEST CASE 4:
        //findAnagrams("azazazazazazzzzaaazazazpp", "az");
    }
    
    public static void findAnagrams(String s, String p)
    {
        StringBuilder sb;
        StringBuilder temp=new StringBuilder();
        int windowSize=0;
        int numStore=0;
        int numMatches=0;
        
        //this will store all the startPos and the maximum window size in which all characters has matched
        //(String p and String s)
        //The lowest maximum will be presented to end user.
        int[][] storeIndexes = new int[s.length()][3];
        sb=new StringBuilder(p);
        int startPos=0;
        boolean hasCharFound=false;
        int counter=0;
        int maximum=0;
        int indexMatch=0;
        int lastIndexMatch=0;
        int incrementIndex=0;
        
        System.out.println("String (s) to search in: " + s);
        System.out.println("String (p) template word: " + p);
        
        if (s.length()<p.length())
        {
            System.out.println("template word is longer length than main String");
            System.exit(0);
        }
        
        do
        {
            do
            {
                System.out.println("BACK HERE$$$$$");
                startPos=counter;
                
                //we now need to let the temp be entire length of String s from startPos
                temp=new StringBuilder(s.substring(startPos,s.length()));
                System.out.println("*****RESTORING BACKUP OF substring STRINGBUILDER (String s): " + temp);
                
                
                for (int i=startPos; i<temp.length();i++)
                {
                    maximum=0;
                    incrementIndex=0;
                    
                    if(!sb.toString().isEmpty())
                    {
                        //important before it gets to the next startPos
                        System.out.println("\nThis is STARTPOS: " + startPos);
                        System.out.println("This is sb length: " + sb.length());
                        
                        System.out.println("This is current StringBuilder (String s): " + temp.toString());
                    }
                    
                    //it is irrelevant the order that letters match, hence we process pos to be entire length
                    //of the remaining characters in StringBuilder sb 
                    for (int pos=0; pos<=sb.length(); pos++)
                    {
                        //This is now relevant since we need to start at first element in sb(String p) if it finds a match.
                        //We would have deleted a character from it by this point
                        if (hasCharFound)
                        {
                            pos=0;
                        }
                     
                        if (!sb.toString().isEmpty())
                        {
                            System.out.println("value of pos: " + pos);
                            System.out.println("value of sb (StringBuilder): " + sb);
                            System.out.println("value of temp (StringBuilder): " + temp);
                            System.out.println("i: " + i);
                            System.out.println("Checking character: " + sb.toString().charAt(pos) + 
                            "  against the main String index: " + i + "("+temp.charAt(i)+")" 
                            + " TO index: " + (temp.length()-1) +  "("+temp.charAt((temp.length()-1))+")" );
                    
                            //if there is a match
                    
                            System.out.println("SUBSTRING EXAMINED: " + temp);
                            
                            if (temp.toString().indexOf(sb.toString().charAt(pos))!=-1)
                            {
                                lastIndexMatch=indexMatch;
                                indexMatch=(startPos + (temp.toString().indexOf(sb.toString().charAt(pos))));
                                
                                //we only want to perform this action on the second match
                                //ie when one character has been removed from StringBuilder sb
                                if (p.length()>sb.length())
                                {
                                if (indexMatch>=lastIndexMatch)
                                {
                                    incrementIndex=incrementIndex+1;
                                    System.out.println("Incremented index: " + incrementIndex + " due to " + (incrementIndex+1) + "deletions from StringBuilder temp");
                                    
                                }
                                }
                                    System.out.println("ADJUSTED INDEX");
                                    System.out.println("char found: " + sb.toString().charAt(pos) + 
                                "    at String s index: " + (startPos + (temp.toString().indexOf(sb.toString().charAt(pos))) + incrementIndex));
                            
                                System.out.println("-------------------------------------------CHECKING TO SEE IF: " + (startPos + (temp.toString().indexOf(sb.toString().charAt(pos)) + incrementIndex)) + " is greater than current maximum: " + maximum);
                                
                                if ((startPos + (temp.toString().indexOf(sb.toString().charAt(pos))+incrementIndex))>maximum)
                                {
                                    //we have taken the maximum index by using improvisation mentioned
                                    maximum=(startPos + (temp.toString().indexOf(sb.toString().charAt(pos) + incrementIndex)));
                                    System.out.println(temp.toString().indexOf(sb.toString().charAt(pos)));
                                    System.out.println("NEW MAXIMUM: " + maximum  + "\t\t"+temp.toString().indexOf(sb.toString().charAt(pos) + "  " + incrementIndex));
                                }
                            
                                if (temp.toString().indexOf(sb.toString().charAt(pos))!=-1)
                                {
                                    System.out.println("char found: " + temp.charAt(temp.toString().indexOf(sb.toString().charAt(pos))) + 
                                "    in temp substring at index: " + (temp.toString().indexOf(temp.charAt(temp.toString().indexOf(sb.toString().charAt(pos))))));
                                    
                                    System.out.println(temp.charAt(temp.toString().indexOf(sb.toString().charAt(pos))) + " has been removed from StringBuilder (String s)" +  "= "+ temp.toString());
                                    
                                    temp.deleteCharAt(temp.toString().indexOf(sb.toString().charAt(pos)));
                                    System.out.println("This is current StringBuilder (String s): " + temp.toString());
                                }
                                
                                //it removes character from the StringBuilder(which holds String p) 
                                //at index which it found the match
                                System.out.println(sb.toString().charAt(pos) + " has been removed from StringBuilder (String p)" +  "= "+ sb.toString());
                                sb.deleteCharAt(pos);
                                //startPos++;
                                System.out.println("This is current StringBuilder (String p): " + sb);
                                System.out.println("HERE EXPECTED MAX: " + maximum);
                                
                                
                                //We know pos for loop will run from 0 to temp.length(entire String s)
                                //So will need to clear contents of StringBuilder temp also
                                //if sb is empty
                                
                                if (sb.toString().isEmpty())
                                {
                                    temp.delete(0,temp.length());
                                    i=temp.length(); //this will also break outer for loop
                                }
                                
                                
                                /*
                                //it removes character from the StringBuilder(which holds String s) 
                                //at index which it found the match
                                System.out.println(sb.toString().charAt(pos) + " has been removed from StringBuilder (String p)" +  "= "+ sb.toString());
                                sb.deleteCharAt(pos);
                                //startPos++;
                                System.out.println("This is current StringBuilder (String p): " + sb.toString());
                                */
                                
                                
                                hasCharFound=true;
                            }
                        //if there is no match found, no need to iterate inner for loop any further
                        //but the outer loop is still relevant since it might find 
                        //anagram starting from one position further in the main string
                
                            else
                            {
                                System.out.println("NO MATCH FOUND");
                                pos=p.length();
                        
                                //I have removed this since we can perform this operation when the inner do while loop exits
                                //(when the StringBuilder is empty)
                                //startPos++;
                        
                                System.out.println("StringBuilder being emptied: " + sb.toString());
                                
                                //empty contents of sb
                                sb.delete(0,sb.length());
                                hasCharFound=false;
                                
                                break;
                            }
                        }
                        System.out.println("1MAXIMUM!!!!!: " + maximum);
    
                    }
                    System.out.println("2MAXIMUM!!!!!: " + maximum);
                    
                    if(!sb.toString().isEmpty())
                    {
                        hasCharFound=false;
                        System.out.println("value of i: " + i);
                        System.out.println("value of startPos: " + startPos);
                    }
                    
                } //end of for loop    for (int i=startPos; i<s.length();i++)
            
                //I am changing the exit condition to this...
                //So this effectively means that any areas of code above where it doesn't find an anagram, 
                //it should in practice remove all contents of the StringBuilder.
                //And only once it leaves the while loop, it should aim to restore it again...
            }while(!sb.toString().isEmpty()  /*startPos<=p.length()*/);
        
            //at this point we know that it has found an anagram
            //since all characters have been removed
            
            //we no longer check for anagram. We are just interested in if 
            //sb.toString().isEmpty()
            //The following is optional: hasCharFound
            
            System.out.println("MAXIMUM HERE------------: " + maximum);
            if (sb.toString().isEmpty())
            {
                System.out.println("max: " + maximum);
                System.out.println("counter:" + counter);
                
                windowSize = ((maximum-startPos)+1);
                
                
                System.out.println("**********************************************************");
                System.out.println("Window " + "size("+((maximum-startPos)+1)+")" + s.substring(startPos,(maximum+1)) + " contains following characters of String p: " + p 
                + "\t\t\tIndex("+counter+" - " + maximum+")");
                System.out.println("**********************************************************");
                                //This would be perfect opportunity to store all maximum
                //We also need to store the startPos....
                //once we compare the maximums and present lowest, we have all information to present information again
                //as above......
                
                //we know that in array each element whether one or two will both have the same maximum in it
                //this is how we check if we should store any more entries
                
                if (startPos==0)
                {
                    System.out.println("3Stored: Window size:" + windowSize + "  " + "start position: " + startPos + " in storeIndexes");
                    storeIndexes[numStore][0]=startPos;
                    storeIndexes[numStore][1]=windowSize;
                     storeIndexes[numStore][2]=maximum;
                    
                    numStore++;
                }
                else
                {
                    
                if (windowSize<storeIndexes[0][1])
                {
                    storeIndexes=new int[s.length()][3];
                    System.out.println("Emptied storeIndexes");
                    numStore=0;
                     System.out.println("2Stored: Window size:" + windowSize + "  " + "start position: " + startPos + " in storeIndexes");
                    storeIndexes[numStore][0]=startPos;
                    storeIndexes[numStore][1]=windowSize;
                    storeIndexes[numStore][2]=maximum;
                    
                    numStore++;
                }
                if (windowSize==storeIndexes[0][1])
                {
                    System.out.println("Entries in array with same window size : + storeIndexes[numStore][1]");
                     System.out.println("1Stored: Window size:" + windowSize + "  " + "start position: " + startPos + " in storeIndexes");
                    storeIndexes[numStore][0]=startPos;
                    storeIndexes[numStore][1]=windowSize;
                    storeIndexes[numStore][2]=maximum;
                    numStore++;
                }
                }
                
                
            }
        
            //we need to restore the original StringBuilder to check further in the String s
        
            System.out.println("*****RESTORING BACKUP OF STRINGBUILDER (String p): " + p);
            sb=new StringBuilder(p);
        
            hasCharFound=false;
        
            //we also need to make the startPos to be i+1
            //since we are now ready to start anagram check at next position in main String
            //since i has been affected in past execution in relation to startPos, 
            //there should be a counter variable running
            //inside the main for loop... This variable would be incremented.
        
            counter++;
        
            //this while statement is so that if there are insufficient characters left in s
            //in relation to length of p, there is no point of performing anagram check
            //it is extremely tricky to know if this statement is 100% index perfect.
            //but all are referencing non-zero indexed based values..
            //startPos is no longer zero indexed based when its context is taken out of the indexing above.
        
        }while(p.length()+startPos<s.length());
        
        int count;
        
        System.out.println("\n\n********** RESULTS *************************************************");
    
        //These are where index values are stored
                //storeIndexes[numStore][0]=startPos;
                //storeIndexes[numStore][1]=windowSize;
                //storeIndexes[numStore][2]=maximum;
                
        System.out.println("checking to see if values in array");
        for (int[] tmp: storeIndexes)
        {
            for (int m: tmp)
            {
                System.out.println(m);
            }
        }       
        
        for (int tmp=0; tmp<numStore; tmp++)
        {
            System.out.println(s);
            startPos=storeIndexes[tmp][0];
            System.out.println("start: " + startPos);
            windowSize=storeIndexes[tmp][1];
            System.out.println("window: " + windowSize);
            maximum=storeIndexes[tmp][2];
            System.out.println("maximum: " + maximum);
                
                System.out.println("**********************************************************");
                System.out.println("Window " + "size("+windowSize+") " + s.substring(startPos,(maximum+1)) + "    contains following characters of String p: " + p 
                + "\t\t\tIndex("+startPos+" - " + maximum+")");
                System.out.println("**********************************************************");
        }
    }
}