astro86 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I need your help on this one as it's been driving me nuts for the last few days. Please note that I have simplified things a bit, but I have a strings that look like this:
A B _ B A A A

I need to find all the unique ways I can remove, in order, the tokens contained in:
A B A

Such that I get the following results, where the 'X' is a removed token from my token string: X X _ B X A A
X X _ B A X A
X X _ B A A X
X B _ X X A A
X B _ X A X A
X B _ X A A X

Which yields 2 unique paths these can be removed:
B _ A A
_ B A A

Any help or hints on how to accomplish this would be very helpful.
Thank you.

Replies are listed 'Best First'.
Re: Finding unique deletion strings
by Anonymous Monk on Sep 08, 2016 at 02:56 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1171353 use strict; use warnings; my $have = 'A B _ B A A A'; my $remove = 'A B A'; my @left; my %left; my $pattern = $remove =~ s/ /(.*)/gr; $have =~ /^$pattern$(?{ push @left, "$`$1$2$'" })(*FAIL)/; s/ +/ /g, s/^ //, s/ $//, $left{$_}++ for @left; print for sort keys %left;
      That was fast and spot on! Well done and thank you!