How to resolve the algorithm Count occurrences of a substring step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Count occurrences of a substring step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Create a function,   or show a built-in function,   to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments:

It should return an integer count. The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left   (see proof on talk page).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Count occurrences of a substring step by step in the PARI/GP programming language

Source code in the pari/gp programming language

subvec(v,u)={
	my(i=1,s);
	while(i+#u<=#v,
		for(j=1,#u,
			if(v[i+j-1]!=u[j], i++; next(2))
		);
		s++;
		i+=#u
	);
	s
};
substr(s1,s2)=subvec(Vec(s1),Vec(s2));
substr("the three truths","th")
substr("ababababab","abab")

  

You may also check:How to resolve the algorithm Sort a list of object identifiers step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Picat programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the E programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the VBA programming language
You may also check:How to resolve the algorithm Safe primes and unsafe primes step by step in the AppleScript programming language