sub find_once { my ( $str , ... ) = @_ ; ... my ( $pos , $once ) = ( 0 ) x2; while ( my $p = get_permutation_string() ) { last if $once > 1; ( $pos = index $str , $p , $pos ) >= 0 and $once++ ; } return $once == 1; } #### #!/usr/local/bin/perl use warnings; use strict; use Algorithm::Permute; use Algorithm::Combinatorics qw/ permutations /; use Math::Combinatorics; my @in = qw( abc cab bac acba acbac acbacb acbXa acbXac acbXacb acbaXc acbaXcb acbacXb acbacbX ) ; my @need = qw( a b c ); printf "%10s ok? %s\n" , "'$_'" # Feel free to switch to any other function. , !!find_once_AC( $_ , @need ) ? 'yes' : 'no' for @in ; exit; # Using Algorithm::Permute. sub find_once_AP { my ( $str , @char ) = @_; my $permute = Algorithm::Permute->new( [ @char ] ); my ( $pos , $once ) = ( 0 ) x 2; while ( my $p = join '' , $permute->next ) { last if $once > 1; ( $pos = index $str , $p , $pos ) >= 0 and $once++ ; } return $once == 1; } # Using Math::Combinatorics. sub find_once_MC { my ( $str , @char ) = @_; my $permute = Math::Combinatorics->new( 'data' => [ @char ] ); my ( $pos , $once ) = ( 0 ) x 2; while ( my $p = join '' , $permute->next_permutation ) { last if $once > 1; ( $pos = index $str , $p , $pos ) >= 0 and $once++ ; } return $once == 1; } # Using Algorithm::Combinatorics. sub find_once_AC { my ( $str , @char ) = @_; my $permute = permutations( [ @char ] ); my ( $pos , $once ) = ( 0 ) x 2; while ( my $p = $permute->next ) { $p = join '' , @{ $p }; last if $once > 1; ( $pos = index $str , $p , $pos ) >= 0 and $once++ ; } return $once == 1; }