Untitled
unknown
c_cpp
2 years ago
591 B
8
Indexable
#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
}
Editor is loading...
Leave a Comment