0% completed
In C++ sollution, we don't need to transform the string to an array of chars for...
Athanasios Petsas
Feb 17, 2022
In C++ sollution, we don't need to transform the string to an array of chars for doing the upper/lower case change and then transform it back to add it to the result vector; we can just do it in place. Here's the code having this change:
{code} static vector findLetterCaseStringPermutations(const string& str) { vector permutations; if (str == "") { return permutations; }
permutations.push_back(str); // process every character of the string one by one for (int i = 0; i < str.length(); i++) { if (!isalpha(str[i])) // only process characters, skip digits continue; // we'll take all existing permutations and change the letter case appropriately int n = permutations.size(); for (int j = 0; j < n; j++) { string newPerm = permutations[j]; // if the current char is in upper case change it to lower case or vice versa if (isupper(newPerm[i])) { newPerm[i] = tolower(newPerm[i]); } else { newPerm[i] = toupper(newPerm[i]); } permutations.push_back(string(newPerm)); }
} return permutations; } {code}
0
0