/*
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
{
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //test cases
        //String text="he##l#hel#llo";
        //String text="major# spar##ks";
        //String text="si###t boy";
        //String text="####";
        //String text="sit boy A#mit Amlan##i"; //PASS
        //String text="#sit boy A#mit Amlan##i"; //FAIL
        
        //String text="#test";  //PASS
        String text="#test again A#mit"; //PASS 
        //String text="#test again A##mit"; // FAILS - double ##
        //String text="t#est again A##mit"; // PASS
        //String text="te##sting"; //PASS
        
        //String text="s#it boy####";  //FAIL   - giving  it #
        
        //it is failing on ## in a multi-word test case
        
        //String text="#test a##gain A#mit"; // PASS
        
        //String text="J#o#h#n"; //PASS
        
        
        System.out.println("This is final string:" + erase(text));
        System.out.println("This is original string:" + text);
    }
    
    public static String erase(String text)
    {
        int i=0;  //iterate through StringBuilder
        StringBuilder sb = new StringBuilder(text); //places text into
        int indexLastHash;
        int indexFirstHash;
        
        /*
        while (sb.charAt(0)=='#')
            {
                //removes first character
                sb.delete(0,1);
            }
            */
        
        System.out.println("This is the string to be examined: " + sb);
        try
        {
            do
            {
                //System.out.println("****" + sb.indexOf("#"));
                //System.out.println("****" + sb.lastIndexOf("#"));
                
                System.out.println("value of i: " + i);
                System.out.println("CURRENT TEXT: " + sb);
                //System.out.println("This22 is last index of #:" + sb.lastIndexOf("#"));
                indexLastHash = sb.lastIndexOf("#");
                
                try
                {
                    if (sb.charAt(indexLastHash-1)!='#')
                    {
                        sb.delete(indexLastHash-1, indexLastHash+1);
                        System.out.println("UP HERE1");
                        
                    }
                    
                    //if there is # before the existing one.
                    //it will attack text from other direction.
                    else
                    {
                        System.out.println("UP HERE2");
                        System.out.println(sb);
                        //since it is first hash, there can not be a hash on left hand side
                        indexFirstHash = sb.indexOf("#");
                        System.out.println(sb.indexOf("#"));
                
                        //if (indexFirstHash!=0)
                        //{
                            //sb.delete(indexFirstHash,indexFirstHash+1);
                        //}
                        //else
                        //{
                            sb.delete(indexFirstHash, indexFirstHash+1);
                        //}
                        
                        System.out.println("*OUTCOME***: " + sb);
                        
                    }
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("Reached first char");
                    System.out.println(sb);
                }
                
                i++;
                
            }while (i!=sb.length());
            
        }
        catch (StringIndexOutOfBoundsException e)
        {
            
            System.out.println("Complete");
            System.out.println(sb);
        
            //this is the scenario described in the challenge to remove any #
            //if there are excess # in relation to characters prior to it
        
            //This should deal with example 3 and example 4
            
            System.out.println(sb.charAt(0));
            System.out.println(sb.indexOf("#"));
            
            try
            {
                
            System.out.println("before entering clear # first : " + sb);
            if (sb.indexOf("#")!=-1)
            {
            do
            {
                System.out.println("INSIDE");
                System.out.println(sb);
                //removes first character
                sb.delete(0,1);
            } while((sb.charAt(0)=='#'));
            }
            
            }
            catch (StringIndexOutOfBoundsException s)
            {
                System.out.println("sds");
                return sb.toString();
            }
            
        }
        
        return sb.toString();
        
    }
}