#include <string>
#include <iostream>
#include <vector>
usingnamespace std;
string globStr;
vector<string>subStrings;
void permutateTheStringsRecur(string str, string res) {
if (res != ""){
cerr << "res: " << res << endl;
bool inOrder = true;
int last = 0;
for (int i = 0; i < globStr.length(); i++){
for (int a = 0; a < res.length(); a++){
if (res[a] == globStr[i]){
if (a < last){
inOrder = false;
}
last = a;
}
}
}
if (inOrder){
cerr << "IN ORDER!" << endl;
subStrings.push_back(res);
}
}
for (int i = 0; i < str.length(); i++){
permutateTheStringsRecur(string(str).erase(i, 1), res + str[i]);
}
}
int main() {
string str;
cin >> str;
globStr = str;
permutateTheStringsRecur(str, "");
int alternatingSubStrings = 0;
cerr << "size of substrings: " << subStrings.size() << endl;
for (int i = 0; i < subStrings.size(); i++){
cerr << "Processing substring: " << subStrings[i] << endl;
bool inOrder = true;
string subStr = subStrings[i];
int randomInt = 0;
for (int a = 0; a < subStr.length(); a++){
int cur = subStr[a];
//cerr << "cur: " << cur << endl;
int next = a + 1;
if (next < subStr.length()){
int nextCur = subStr[next];
if (cur > nextCur && randomInt == 0){
inOrder = false;
}
elseif (cur < nextCur && randomInt == 1){
inOrder = false;
}
if (randomInt == 0){
randomInt = 1;
}
else{
randomInt = 0;
}
}
else{
break;
}
}
if (inOrder){
cerr << "SUBSTRING IN ORDER" << endl;
alternatingSubStrings++;
}
}
cout << alternatingSubStrings % 1000000007;
return 0;
}
Basically I have to find all the permutations of a string but I cannot change the order, so abcde can be abde or abc or ae but not aedb
Then I have to check if the substring follows this pattern, first letter ascii value < second letter ascii value. second letter ascii value > third letter ascii value. third letter ascii value < fourth ascii value.