/*
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(s);
        StringBuilder sbBackup = new StringBuilder(s);
        int startPos=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
            for (int i=startPos; i<s.length();i++)
            {
                
                for (int pos=0; pos<sb.length(); pos++)
                {
                    
                    System.out.println("Checking character: " + sb.toString().charAt(pos) + "in the main String index: " + i + "("+s.charAt(i));
                    
                    //if there is a match
                    if (s.substring(startPos,p.length()).indexOf(p.charAt(pos))!=-1)
                    {
                        System.out.println("char found: " + p.charAt(pos));
                        
                        //it removes character from the StringBuilder(which holds String p) 
                        //at index which it found the match
                        sb.deleteCharAt(pos);
                        startPos++;
                        System.out.println("This is current StringBuilder (String p): " + sb.toString());
                    }
                    //if there is no match found, no need to iterate inner for loop any further 
                    else
                    {
                        pos=p.length();
                    }
                } 
            }
        }while(startPos<=s.length());
        
        //at this point we know that it has found an anagram
        //since all characters have been removed
        
        if (sb.toString().isEmpty())
        {
            System.out.println(s.substring(startPos,p.length()) + " is an anagram of: " + p);
        }
        
        //we need to restore the original StringBuilder to check further in the String s
        sb=sbBackup;
        
        //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());
    }
}