/*
Online Java - IDE, Code Editor, Compiler

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

public class Main 
{
    static StringBuilder sb;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //TEST CASE 1:
        findAnagrams("cbaebabacd", "abc");
        
        //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;
        
        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
            {
                //this is going through the main String...
                //it is starting more inwards once it has processed the entire length of p
                //or if it fails to find a letter in String p within the main string
                //counter has been incremented in this circumstance outside of the inner do while loop
                //it has to be remembered the outer do while loop is when there are insufficient characters in main String
                //to compensate for String p
            
                startPos=counter;
            
                for (int i=startPos; i<s.length();i++)
                {
                    if(!sb.toString().isEmpty())
                    {
                        System.out.println("\nThis is STARTPOS: " + startPos);
                        System.out.println("This is sb length: " + sb.length());
                    }
                    
                    //we require less than or equal to here
                    //since if the stringbuilder is reduced to a single character (sb.length()=1)
                    //this loop will not increase and remain at 0. 
                    //It will exit the for loop and cause the outer for loop to increase by 1..
                    //So it will impact the screen output to be incorrect for
                    // System.out.println("Checking character: " + sb.toString().charAt(pos) + 
                    //"  against the main String index: " + i + "("+s.charAt(i)+")" 
                    //It will show incorrect string index.
                    //HOWEVER IT WILL NOT IMPACT THE indexOf().  
                    //Even though indexOf relies on the startPos, we can see from above that the value of StartPos
                    //(startPos=counter)
                    //has only changed when it has broken out of the most outer for loop. So this remains unaffected
                    
                    for (int pos=0; pos<sb.length(); pos++)
                    {
                        if (hasCharFound)
                        {
                            pos=0;
                        }
                    
                        //This is included since it will attempt to perform charAt(pos) on an empty StringBuilder 
                        if (!sb.toString().isEmpty())
                        {
                            System.out.println("value of pos: " + pos);
                            System.out.println("value of sb: " + sb.toString());
                            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: " + (i+(p.length()-1)) +  "("+s.charAt((i + (p.length()-1)))+")" );
                    
                            //if there is a match
                    
                            System.out.println("SUBSTRING EXAMINED: " + s.substring(startPos,(startPos+p.length())));
                            
                            if (s.substring(startPos,(startPos+p.length())).indexOf(sb.toString().charAt(pos))!=-1)
                            {
                                //System.out.println("char found: " + s.charAt(s.substring(startPos,p.length()).indexOf(p.charAt(pos))));
                        
                                System.out.println("char found: " + sb.toString().charAt(pos) + 
                                "    at index: " + (startPos + s.substring(startPos,(startPos+p.length())).indexOf(sb.toString().charAt(pos))));
                                
                                //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.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());
                                sb.delete(0,sb.length());
                                hasCharFound=false;
                                break;
                            }
                        }
                    }
                    if(!sb.toString().isEmpty())
                    {
                        System.out.println("REACH1");
                        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()*/);
        
            System.out.println("REACH2: " + startPos);
            //at this point we know that it has found an anagram
            //since all characters have been removed
        
            if (sb.toString().isEmpty() && hasCharFound)
            {
                System.out.println("**********************************************************");
                System.out.println(s.substring(counter,(counter+p.length())) + " is an anagram of: " + p 
                + "\t\t\tIndex("+counter+")");
                System.out.println("**********************************************************");
            }
        
            //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());
    }
}

