LeetCode 809: Expressive Words I'm posting my C++ code for LeetCode's Expressive Words. If you have time and would like to review, please do so. Thank you!

Sometimes people repeat letters to represent extra feeling, such as
&quot;hello&quot; -&gt; &quot;heeellooo&quot;, &quot;hi&quot; -&gt; &quot;hiiii&quot;.  In these strings like
&quot;heeellooo&quot;, we have groups of adjacent letters that are all the same:
&quot;h&quot;, &quot;eee&quot;, &quot;ll&quot;, &quot;ooo&quot;.
For some given string S, a query word is stretchy if it can be made to
be equal to S by any number of applications of the following extension
operation: choose a group consisting of characters c, and add some
number of characters c to the group so that the size of the group is 3
or more.
For example, starting with &quot;hello&quot;, we could do an extension on the
group &quot;o&quot; to get &quot;hellooo&quot;, but we cannot get &quot;helloo&quot; since the group
&quot;oo&quot; has size less than 3.  Also, we could do another extension like
&quot;ll&quot; -&gt; &quot;lllll&quot; to get &quot;helllllooo&quot;.  If S = &quot;helllllooo&quot;, then the
query word &quot;hello&quot; would be stretchy because of these two extension
operations: query = &quot;hello&quot; -&gt; &quot;hellooo&quot; -&gt; &quot;helllllooo&quot; = S.
Given a list of query words, return the number of words that are
stretchy.
Example: Input:  S = &quot;heeellooo&quot; words = [&quot;hello&quot;, &quot;hi&quot;, &quot;helo&quot;]
Output: 1 Explanation:  We can extend &quot;e&quot; and &quot;o&quot; in the word &quot;hello&quot;
to get &quot;heeellooo&quot;. We can't extend &quot;helo&quot; to get &quot;heeellooo&quot; because
the group &quot;ll&quot; is not size 3 or more.
Notes:
0 &lt;= len(S) &lt;= 100.
0 &lt;= len(words) &lt;= 100.
0 &lt;= len(words[i]) &lt;= 100.
S and all words in words consist only of lowercase letters

Accepted C++
class Solution {
public:
    int expressiveWords(std::string base_string, std::vector&lt;string&gt; &amp;words) {
        int count = 0;

        for (auto &amp;word : words)
            if (is_stretchable(base_string, word))
                count++;

        return count;
    }

protected:
    bool is_stretchable(std::string base_string, std::string words) { // 4 pointers
        int base_length = base_string.size();
        int words_length = words.size();
        int left_a = 0, right_a = 0;

        for (int left_b  = 0, right_b = 0; left_a &lt; base_length &amp;&amp; right_a &lt; words_length; left_a = left_b, right_a = right_b) {
            if (base_string[left_a] != words[right_a])
                return false;

            while (left_b &lt; base_length &amp;&amp; base_string[left_b] == base_string[left_a])
                left_b++;

            while (right_b &lt; words_length &amp;&amp; words[right_b] == words[right_a])
                right_b++;

            if (left_b - left_a != right_b - right_a &amp;&amp; left_b - left_a &lt; std::max(3, right_b - right_a))
                return false;
        }

        return left_a == base_length &amp;&amp; right_a == words_length;
    }
};

Reference
On LeetCode, there is an instance usually named Solution with one or more public functions which we are not allowed to rename those.

Solution
Discussion
Question
Pass by const reference to prevent copying and modification:
int expressiveWords(std::string const&amp; base_string, std::vector&lt;string&gt; const&amp; words)
                                ^^^^^^                                  ^^^^^^

bool is_stretchable(std::string const&amp; base_string, std::string const&amp; words)
                                ^^^^^^                          ^^^^^^


    for (auto const&amp; word : words)
              ^^^^^^

This prevents the strings being copied into the function. It will also catch situations were you accidentally try and modify the string.

Please add braces {} around sub blocks.
It will save you one day without you even knowing. Issues caused by missing braces are notoriously hard to find because they are caused when sombody else does something nasty that affects your code (multi line macros come to mind).

One variable per line:
   int left_a = 0, right_a = 0;

Its not going to hurt you to add an extra line and it makes the job easier for the next person.

Not sure it is worth it.
But just to be complete you could use a standard algorithm to count.
    int count = 0;

    for (auto &amp;word : words)
        if (is_stretchable(base_string, word))
            count++;

    return count;

    // ---

    return std::count(std::begin(words), std::end(words),
                      [&amp;base_string, this](auto const&amp; word){return is_stretchable(base_string, word);});