/*
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";  //TEST CASE 1: PASS   1D,1D,2D
        //String text="major# spar##ks";  //TEST CASE 2: PASS 2D,2D,1D
        //String text="si###t boy";  //TEST CASE 3: PASS  2D, 2D, 2R
        //String text="####";  //TEST CASE 4: PASS  1R,2R
        //String text="sit boy A#mit Amlan##i"; //TEST CASE 5: PASS  2D,2D,1D
        //String text="#sit boy A#mit Amlan##i"; //TEST CASE 6: PASS  1R,2D,1D
        //String text="#test";  //TEST CASE 7: FAIL, NOW PASSING  5R
        //String text="#test again A#mit"; //TEST CASE 8: PASS  1D, 2R
        //String text="#test again A##mit"; //TEST CASE 9: PASS  1D, 2D, 1R
        //String text="t#est aga###in A##mit"; //TEST CASE 10: PASS  1D, 2D
        //String text="t#est again A##mit"; //TEST CASE 11: PASS  1D, 2D
        //String text="te##sting"; //TEST CASE 12: PASS  1D, 2D
        //String text="#test a###gain A#mit##"; //TEST CASE 13: PASS 1R, 1D, 2D
        //String text="s#it boy####";  //TEST CASE 14: PASS  2D, 3D
        
        //String text="J#o#h#n"; //UNDOCUMENTED TEST CASE:  PASS  1D
        //String text = "This #i#s# a v###r#y lo#n##g###sentence t#o# s#e#e# t#h### ##c#o#d#e#.";  //1D, 2D
        //String text="   ###";  //2D, 3D
        
        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;  
        StringBuilder sb = new StringBuilder(text);
        int indexLastHash;
        int indexFirstHash;
        int count=0; 
        
        System.out.println("This is the string to be examined: " + sb);
        
        try
        {
            do
            {
                indexLastHash = sb.lastIndexOf("#");
                indexFirstHash=sb.indexOf("#");    
                try
                {
                    if (sb.charAt(indexLastHash-1)!='#')
                    {
                        System.out.println("1Deleting character: " + sb.charAt(indexLastHash-1) + " at index: " + (indexLastHash-1));
                        sb.delete(indexLastHash-1, indexLastHash+1);
                        System.out.println("Formatted text: " + "<"+sb+">");
                    }
                    else
                    {
                        indexFirstHash = sb.indexOf("#");
                        
                        if (indexFirstHash!=0)
                        {
                            System.out.println("2Deleting character: " + sb.charAt(indexFirstHash-1) + " at index: " + (indexFirstHash-1));
                            sb.delete(indexFirstHash-1, indexFirstHash+1);
                            System.out.println("Formatted text: " +"<"+sb+">");
                        }
                        else
                        {
                            System.out.println("1Removing ineffective BACKSPACE at index: " + indexFirstHash);
                            sb.delete(indexFirstHash, indexFirstHash+1);
                            System.out.println("Formatted text: " + "<"+sb+">");
                        }
                    } //end of else statement
                    
                    if (sb.indexOf("#")==0)
                    {
                        do
                        {
                            System.out.println("2Removing ineffective BACKSPACE at index 0");
                            sb.delete(0,1);
                            System.out.println("Formatted text: " + sb);
                            
                        }while((sb.charAt(0)=='#'));
                    }
                    
                } //end try
                catch (ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("SHOULD NOT REACH HERE");
                }
                i++;  // counter for indexing
                
            }while (i!=sb.length());
            
            if (sb.indexOf("#")!=-1)
            {
                do
                {
                    if (sb.indexOf("#")==0)
                    {
                        System.out.println("3Removing ineffective BACKSPACE at index 0");
                        sb.delete(sb.indexOf("#"),sb.indexOf("#")+1);
                    }
                    else
                    {
                        System.out.println("3Deleting character: " + sb.charAt((sb.indexOf("#")-1)) + " at index: " + ((sb.indexOf("#")-1)));
			            sb.delete((sb.indexOf("#")-1), (sb.indexOf("#")+1));
                    }
                    System.out.println("Formatted text: " + sb);
                    count++;
                } while((count<sb.length()));
            }
        }  //end of main try block
        catch (StringIndexOutOfBoundsException e)
        {
            if (sb.indexOf("#")!=-1)
            {
                do
                {
                    System.out.println("4Removing ineffective BACKSPACE at index 0");
                    sb.delete(0,1);
                    System.out.println("Formatted text: " + sb);
                    
                } while((sb.charAt(0)=='#'));
            }
            return sb.toString();
        }  //end of catch
        return sb.toString();
    }
}