private static Queue < String > removePrefix ( final List < String > input ) { if ( input . size ( ) < 2 ) { return new LinkedList <> ( input ) ; } String resultingPrefix = "" ; final String previousGreatestCommonPrefix = input . get ( 0 ) . trim ( ) ; String greatestCommonPrefix = "" ; for ( int i = 1 ; i < input . size ( ) ; i ++ ) { final String previousLine = input . get ( i - 1 ) . trim ( ) ; final String currentLine = input . get ( i ) . trim ( ) ; greatestCommonPrefix = ExceptionParser . greatestCommonPrefix ( previousLine , currentLine ) ; resultingPrefix = ExceptionParser . greatestCommonPrefix ( previousGreatestCommonPrefix , greatestCommonPrefix ) ; if ( resultingPrefix . length ( ) == 0 ) { break ; } } final int prefixLength = resultingPrefix . length ( ) ; final boolean hasPrefix = prefixLength > 0 ; final Queue < String > result = new LinkedList <> ( ) ; for ( final String line : input ) { final String line2 = line . trim ( ) ; if ( hasPrefix ) { result . add ( line2 . substring ( prefixLength ) ) ; } else { result . add ( line2 ) ; } } return result ; }
Identifies and removes the common prefix - the longest beginning substring that all the lines share .