Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
591 B
4
Indexable
Never
#include <iostream>
#include <string>

int count_reversible_substrings_to_smaller_lexico_optimized(const std::string& s, int k) {
    int count = 0; // Initialize count of reversible substrings

    // Iterate over the string to get all possible start positions for substrings of length k
    for (int i = 0; i <= static_cast<int>(s.length()) - k; ++i) {
        // Check the first and the last character of the current substring
        if (s[i] > s[i + k - 1]) {
            count += 1; // Increment count if condition is met
        }
    }

    return count; // Return the total count
}
Leave a Comment