/*
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
            {
               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());
                    }
                
                    for (int pos=0; pos<=sb.length(); pos++)
                    {
                        if (hasCharFound)
                        {
                            pos=0;
                        }
                        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)))+")" );
                    
                            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: " + sb.toString().charAt(pos) + 
                                "    at index: " + s.substring(startPos,(startPos+p.length())).indexOf(sb.toString().charAt(pos)));
                               
                                System.out.println(sb.toString().charAt(pos) + " has been removed from StringBuilder (String p)" +  "= "+ sb.toString());
                                sb.deleteCharAt(pos);
                                System.out.println("This is current StringBuilder (String p): " + sb.toString());
                                hasCharFound=true;
                            }
                            else
                            {
                                System.out.println("NO MATCH FOUND");
                                pos=p.length();
                                System.out.println("StringBuilder being emptied: " + sb.toString());
                                sb.delete(0,sb.length());
                                hasCharFound=false;
                                break;
                            }
                        }
                    }
                }
            }while(!sb.toString().isEmpty());
        
            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("**********************************************************");
            }
            System.out.println("*****RESTORING BACKUP OF STRINGBUILDER (String p): " + p);
            
            sb=new StringBuilder(p);
            hasCharFound=false;
            counter++;
        
        }while(p.length()+startPos<s.length());
    }
}