PHP What is the fastest way to count strings occurrences? [closed]

柔情痞子 提交于 2021-02-10 12:44:19

问题


A prefix of a string S is any leading contiguous part of S. For example, "c" and "cod" are prefixes of the string "codility". For simplicity, we require prefixes to be non-empty. The product of prefix P of string S is the number of occurrences of P multiplied by the length of P. More precisely, if prefix P consists of K characters and P occurs exactly T times in S, then the product equals K * T. For example, S = "abababa" has the following prefixes:

"a", whose product equals 1 * 4 = 4,
"ab", whose product equals 2 * 3 = 6,
"aba", whose product equals 3 * 3 = 9,
"abab", whose product equals 4 * 2 = 8,
"ababa", whose product equals 5 * 2 = 10,
"ababab", whose product equals 6 * 1 = 6,
"abababa", whose product equals 7 * 1 = 7.

The longest prefix is identical to the original string. The goal is to choose such a prefix as maximizes the value of the product. In above example the maximal product is 10. In this problem we consider only strings that consist of lower-case English letters (a−z). Write a function

class Solution { public int solution(String S); }

that, given a string S consisting of N characters, returns the maximal product of any prefix of the given string. If the product is greater than 1,000,000,000 the function should return 1,000,000,000. For example, for a string:

S = "abababa" the function should return 10, as explained above,
S = "aaa" the function should return 4, as the product of the prefix "aa" is maximal.

Assume that:

N is an integer within the range [1..300,000];
string S consists only of lower-case letters (a−z).

Complexity:

expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

My code so far:

function solution($S) {
    $PROD = 0;
    for ($i=1; $i <= strlen($S); $i++){
        $p = substr($S, 0, $i);
        $counter = 0;
        $offset = 0;
        $pos = strpos($S, $p, $offset);
        while($pos !== false) { 
            $counter++;
            $offset = $pos + 1;            
            $pos = strpos($S, $p, $offset);
        }
        if ($PROD < ($counter * strlen($p))){
            $PROD = $counter * strlen($p);
            if ($PROD > 1000000000)
                return 1000000000;
        }
    }
    return $PROD;
}

is there any way to do it more faster ?


回答1:


I think you will have to make your own function. This is how I would make it :

The demo

function substr_count_overlap($string, $needle) {
    $count = 0;
    $start = 0;
    while(1) {
        $found = strpos($string, $needle, $start);
        if($found !== FALSE) {
            $count++;
            $start = $found + 1;
        } else return $count;
    }
    return $count;
}

And use it this way :

$myString = 'aaa';
$search = 'aa';

echo substr_count_overlap($myString, $search);

This is faster because of this line :

$start = $found + 1;

You don't walk the entire string 1 char by 1, but you step directly to the next occurence.



来源:https://stackoverflow.com/questions/20269745/php-what-is-the-fastest-way-to-count-strings-occurrences

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!