traversing string from the end .
for ( int i = str . Length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' [ ' ) { List < char > s = new List < char > ( ) ; while ( arr . Peek ( ) != ' ] ' ) { s . Add ( arr . Peek ( ) ) ; arr . Pop ( ) ; } arr . Pop ( ) ;