/*
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 :)");
        
        findAnagrams("cbaebabacd", "abc");
    }
    
    public static void findAnagrams(String s, String p)
    {
        sb=new StringBuilder(p);
        StringBuilder sbBackup = 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);
        
        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++)
            {
                System.out.println("This is STARTPOS: " + startPos);
                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("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("*******: " + p.charAt(pos));
                    if (s.substring(startPos,p.length()).indexOf(p.charAt(pos))!=-1)
                    {
                        //System.out.println("char found: " + s.charAt(s.substring(startPos,p.length()).indexOf(p.charAt(pos))));
                        
                        System.out.println("char found: " + p.charAt(pos) + "    at index: " 
                        + s.substring(startPos,p.length()).indexOf(p.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.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 posit//ion
                    //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;
                    }
                }
            }
                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(s.substring(counter,(counter+p.length())) + " is an anagram of: " + p);
        }
        
        //we need to restore the original StringBuilder to check further in the String s
        sb=sbBackup;
        
        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());
    }
}