#include <iostream>
#include <string>
#include <regex>
// Non‑overlapping occurrences are matches of a substring that do not reuse any of
// the same characters. Once one match is counted, the next search must begin
// after that match ends.
int count(const std::string& s, const std::string& substr) {
/*
Count how many times 'substr' appears in 's'.
This version uses std::regex + std::sregex_iterator.
Note: std::sregex_iterator counts overlapping matches,
just like Scala's Regex.findAllIn.
*/
std::regex pattern(substr);
auto begin = std::sregex_iterator(s.begin(), s.end(), pattern);
auto end = std::sregex_iterator();
return std::distance(begin, end);
}
int main() {
// Demonstration using the string provided in the instructions:
std::string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
// Count occurrences
std::cout << count(s, "php") << std::endl;
return 0;
}
/*
run:
6
*/