/*
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#";
        
        
        System.out.println("This is original string:" + text);
        System.out.println("This is final string:" + erase(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);
            }
            */
        
        
        try
        {
            do
            {
                System.out.println("This is last index of #:" + sb.lastIndexOf("#"));
                indexLastHash = sb.lastIndexOf("#");
                
                try
                {
                    if (sb.charAt(indexLastHash-1)!='#')
                    {
                        sb = sb.delete(indexLastHash-1, indexLastHash+1);
                        
                    }
                    else
                    {
                        System.out.println("should be here");
                        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);
                        System.out.println("first index hash: " + indexFirstHash);
                
                        sb = sb.delete(indexFirstHash-1, indexFirstHash+1);
                        
                    }
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("Reached first char");
                    System.out.println(sb);
                }
                
                i++;
                
            }while (i!=text.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
            {
                
            
            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();
        
    }
}