Skip to main content
edited body
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65

Improved version of your function (using C++17 std::string_view as the Parameterparameter to avoid any copies, even in the caller, whether he has a std::string or not):

Improved version of your function (using C++17 std::string_view as the Parameter to avoid any copies, even in the caller, whether he has a std::string or not):

Improved version of your function (using C++17 std::string_view as the parameter to avoid any copies, even in the caller, whether he has a std::string or not):

copy-edited; simplified and shortened code
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65

Making copies, reversing, and then comparing is just superfluous additional work.

Improved version of your function (using C++17 std::string_view as the Parameter to avoid any copies, even in the caller, whether he has a std::string or not):

long substrCount(std::string_view s) noexcept {
    char c[3] = {};
    long n[3] = {};
    long r = 0;
    for (auto curr = begin(s), last = end(s), pos = begin(s); curr != last; curr = pos) {
        pos = std::find_if(curr + 1, last, [=](char c){ return c != *curr; });
        std::tie(n[2] = n[1];
       , n[1] = n[0];
    , n[0]) = std::make_tuple(n[1], n[0] =, pos - curr;curr);
        std::tie(c[2] = c[1];
       , c[1] = c[0];
    , c[0]) = std::make_tuple(c[1], c[0] =, *curr;*curr);
        r += *n * (*n + 1) / 2;
        if (n[1] == 1 && c[0] == c[2])
            r += std::min(n[0], n[2]);
    }
    return r;
}

Making copies, reversing, and is just superfluous additional work.

Improved version of your function (using C++17 std::string_view to avoid any copies):

long substrCount(std::string_view s) noexcept {
    char c[3] = {};
    long n[3] = {};
    long r = 0;
    for (auto curr = begin(s), last = end(s), pos = begin(s); curr != last; curr = pos) {
        pos = std::find_if(curr + 1, last, [=](char c){ return c != *curr; });
        n[2] = n[1];
        n[1] = n[0];
        n[0] = pos - curr;
        c[2] = c[1];
        c[1] = c[0];
        c[0] = *curr;
        r += *n * (*n + 1) / 2;
        if (n[1] == 1 && c[0] == c[2])
            r += std::min(n[0], n[2]);
    }
    return r;
}

Making copies, reversing, and then comparing is just superfluous additional work.

Improved version of your function (using C++17 std::string_view as the Parameter to avoid any copies, even in the caller, whether he has a std::string or not):

long substrCount(std::string_view s) noexcept {
    char c[3] = {};
    long n[3] = {};
    long r = 0;
    for (auto curr = begin(s), last = end(s), pos = begin(s); curr != last; curr = pos) {
        pos = std::find_if(curr + 1, last, [=](char c){ return c != *curr; });
        std::tie(n[2], n[1], n[0]) = std::make_tuple(n[1], n[0], pos - curr);
        std::tie(c[2], c[1], c[0]) = std::make_tuple(c[1], c[0], *curr);
        r += *n * (*n + 1) / 2;
        if (n[1] == 1 && c[0] == c[2])
            r += std::min(n[0], n[2]);
    }
    return r;
}
added 47 characters in body
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
long substrCount(std::string_view s) noexcept {
    char c[3] = {};
    long n[3] = {};
    long r = 0;
    for (auto countcurr = [&]{
        r += *n * begin(*n + 1s) / 2;
   , last = end(s), pos if= begin(n[1]s); ==curr 1!= &&last; c[0]curr === c[2]pos)
    {
        rpos +== std::minfind_if(n[0], n[2]);
    };
 curr + 1, forlast, [=](auto x :char sc) {
       return ifc (x!= ==*curr; *c}) {;
          n[2] = ++*n;n[1];
        }n[1] else= {n[0];
        n[0] = pos - count();curr;
            c[2] = c[1];
            c[1] = c[0];
            c[0] = x;*curr;
         r += *n n[2]* =(*n n[1];
+ 1) / 2;
        if (n[1] = n[0];
  == 1 && c[0] == c[2])
     n[0] = 1;
     r += std::min(n[0], }n[2]);
    }
    count();
    return r;
}
long substrCount(std::string_view s) noexcept {
    char c[3] = {};
    long n[3] = {};
    long r = 0;
    auto count = [&]{
        r += *n * (*n + 1) / 2;
        if (n[1] == 1 && c[0] == c[2])
            r += std::min(n[0], n[2]);
    };
    for (auto x : s) {
        if (x == *c) {
            ++*n;
        } else {
            count();
            c[2] = c[1];
            c[1] = c[0];
            c[0] = x;
            n[2] = n[1];
            n[1] = n[0];
            n[0] = 1;
        }
    }
    count();
    return r;
}
long substrCount(std::string_view s) noexcept {
    char c[3] = {};
    long n[3] = {};
    long r = 0;
    for (auto curr = begin(s), last = end(s), pos = begin(s); curr != last; curr = pos) {
        pos = std::find_if(curr + 1, last, [=](char c){ return c != *curr; });
        n[2] = n[1];
        n[1] = n[0];
        n[0] = pos - curr;
        c[2] = c[1];
        c[1] = c[0];
        c[0] = *curr;
        r += *n * (*n + 1) / 2;
        if (n[1] == 1 && c[0] == c[2])
            r += std::min(n[0], n[2]);
    }
    return r;
}
edited body
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
Loading
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
Loading