/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/
/*
<p>1) at each startPos, re-instantiate a StringBuilder temp which temp=new StringBuilder(s.substring(startPos, startPos + p.length()),  sb= new StringBuilder(p). NOTE: Chosen to place substring in temp since it will make it easier to see screen ouputs of deleted character.</p>
<p>2) check see if character found in the substring as per usual code.</p>
<p>if there is a match, check to see if same character appears in temp. we would need perform temp.toString().indexOf(LETTER MATCHED) != -1</p>
<p>Remove character from temp if there is a match. since this is substring of String s (it will have a match similar to sb (String p). Perform temp.deleteCharAt(temp.toString().indexOf(LETTER MATCHED, CAN BE OBTAINED FROM PREVIOUS SCREEN OUTPUT)</p> 
<p>if there is no match, force a break or maximise p length(). Empty contents of sb, this will break the inner do while loop.  This will force a new startPos. . Re-initialise sb = new StringBuilder(p) and temp=new StringBuilder(s.substring(startPos, startPos + p.length()) </p>
<p>if a match, process step 2 until it exits inner do while loop sb.toString().is empty()</p>
<p>reset variables immediately sb, temp (at commence of startPos) </p>
<p>code will exit when String p is too wide</p>
*/

//OPTION 3 - this keeps the main for loop intact and does not substitute any areas of loop (OPTION 1).
//My first fully functional attempt so minimum changes were complete..  BUT functional

public class Main 
{
    static StringBuilder sb;
    static StringBuilder temp;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //TEST CASE 1:
        findAnagrams("cbacebabacdabc", "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)
    {
        sb=new StringBuilder(p);
        int startPos=0;
        boolean hasCharFound=false;
        int counter=0;
        int maximum=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;
            
                for (int i=startPos; i<s.length();i++)
                {
                    //maximum=0;
                    
                    if(!sb.toString().isEmpty())
                    {
                        System.out.println("\nThis is STARTPOS: " + startPos);
                        System.out.println("This is sb length: " + sb.length());
                        
                        //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);
                        System.out.println("This is current StringBuilder (String s): " + temp.toString());
                    }
                    
                    maximum=0;
                    
                    //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 + "("+s.charAt(i)+")" 
                            + " TO index: " + (s.length()-1) +  "("+s.charAt((s.length()-1))+")" );
                    
                            //if there is a match
                    
                            System.out.println("SUBSTRING EXAMINED: " + s.substring(startPos,s.length()));
                            
                            if (s.substring(startPos,s.length()).indexOf(sb.toString().charAt(pos))!=-1)
                            {
                                System.out.println("char found: " + sb.toString().charAt(pos) + 
                                "    at String s index: " + (startPos + (s.substring(startPos,s.length()).indexOf(sb.toString().charAt(pos)))));
                                
                                System.out.println("-------------------------------------------CHECKING TO SEE IF: " + s.substring(0,s.length()).indexOf(sb.toString().charAt(pos)) + " is greater than current maximum: " + maximum);
                                
                                if ((startPos + (s.substring(startPos,s.length()).indexOf(sb.toString().charAt(pos))))>maximum)
                                {
                                    maximum=(startPos + (s.substring(startPos,s.length()).indexOf(sb.toString().charAt(pos))));
                                    System.out.println("NEW MAXIMUM: " + maximum);
                                }
                                
                            
                                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=s.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())
                    {
                        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);
                
                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("**********************************************************");
                maximum=0;
            }
        
            //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());
    }
}