更新时间:2023-02-26 11:21:11
最简单的方法是将要测试的单词形成一个正则表达式.
This is done most simply by forming a regular expression from the word that is to be tested.
这对可用字符列表进行排序并通过连接它们形成一个字符串.然后将每个候选词拆分为字符、排序并使用正则表达式项 .*
作为分隔符重新连接.因此,例如,abac
将被转换为 a.*a.*b.*c
.
This sorts the list of available characters and forms a string by concatenating them. Then each candidate word is split into characters, sorted, and rejoined with the regex term .*
as separator. So, for instance, abac
will be converted to a.*a.*b.*c
.
然后通过根据派生的正则表达式测试可用字符串来确定单词的有效性.
Then the validity of the word is determined by testing the string of available characters against the derived regex.
use strict;
use warnings;
my @chars = qw/ a b c d a e f g /;
my $chars = join '', sort @chars;
for my $word (qw/ mom dad bad fag abac /) {
my $re = join '.*', sort $word =~ /./g;
print "$word is ", $chars =~ /$re/ ? 'valid' : 'NOT valid', "\n";
}
输出
mom is NOT valid
dad is NOT valid
bad is valid
fag is valid
abac is valid