我正在尝试查找一组字符中特定字符串出现的次数。字符串中的字母不必彼此相邻,而必须按顺序出现。
例如,以下序列包含四个出现的字符串“ abc”
序列“ aabcc”
出现次数:
a a b c c
a a b c c
a a b c c
a a b c c
在不消耗字符的情况下如何进行匹配的任何帮助?我正在尝试使用正则表达式和匹配项,但不确定是否可行。我正在为php编写
答案 0 :(得分:0)
<?php
$sequence = "aabbcc";
$occurrences = array();
$sequences = str_split($sequence);
// dn($sequences);
$sequences_amount = array();
foreach ($sequences as $key => $char) {
if(!isset($sequences_amount[$char])){
$sequences_amount[$char] = 1;
}else{
$sequences_amount[$char]++;
}
}
var_dump($sequences_amount);
function sequenceCount($data = array())
{
if(!isset($data) || empty($data) || (count($data) == 0))return;
$count = NULL;
foreach ($data as $char => $amount) {
if($count == NULL){
$count = $amount;
}else{
$count = $count * $amount;
}
}
return $count;
}
$count = sequenceCount($sequences_amount);
echo("\n\nResult:\n");
echo($count);