Given the problem: Find all cyclic permutations of a string
For example: given a string: "abcd"
All the cyclic permutations of a string would be: "abcd", "dabc", "cdab", "bcda"
Here's what I have tried out:
for(int i = 0; i < str.size(); i++){
permu.push_back(str);
str.insert(0, 1, str[str.size()-1]);
str.erase(str.end()-1);
}
I got Time-limit-exceeded since the insert and erase function takes O(n) making the overall solution O(n^2)
Is there anyway to solve this in O(n) or less?