Print all Permutations of given String: using C++
What do permutations mean?
Permutation is defined as "various arrangements produced out of a certain number of things by taking some or all of them."
For instance, the permutation of the three letters abc is: ab, ba, bc, cb, ac, ca.
In coding contests and various placement exams, strings are frequently asked about. We will discuss "Permutations in String," one of the most frequently asked questions based on strings, in this post.
Problem Proposition:
You are given a string of lowercase letters, "str," to enter. Return every permutation in the string, in any order.
Sample Input :
abc
Sample Output:
cab, cba, bac, acb, and abc
Solution Method :
The permutations of a string can be printed using a variety of algorithms and methods.
Approach-1 Using the Backtrack
A backtracking method attempts to create a solution gradually, one step at a time, and discards any alternatives that do not meet the criteria of the problem at any point in time. Backtracking is a recursive problem-solving strategy.
Backtracking is the best method for printing every variation as a string.
Algorithm
- We'll create a function called generatePermutaionsHelper in the algorithm (Str, l, r). The permutations of the substring produced by this function start at index "l" and end at index "r." Invoking the generatePermutaionsHelper method mentioned above (Str, l, r).
- A new permutation is discovered if "l" equals "r." Put this string in the list of "ans."
- If not, keep repeating the string from "l" to "r."
- Assign the current index to "i."
- To correct the "ith" character on the index "l," swap Str[l] and Str[i].
- To obtain the permutation of the remaining characters, call generatePermutaionsHelper(Str, l + 1, r).
- Go back and switch Str[l] and Str[i] once again.
By the time we're done, the list "ans" will include every possible combination of the supplied string. We must sort the list in order to get the permutations in lexicographically increasing order.
Implementation of Approach-1
#include <bits/stdc++.h> |
Output:
aac |
Time complexity
This method's temporal complexity, where N is the string's length, is O(N!).
Reason :
The reason is that we are randomly creating each of the n! permutations. Thus, it takes O(N!) time to create every variant of a string. Additionally, we are sorting the O(N!)-dimensional "ans" list, which will take O(log(N!)) time.
A final time complexity of O(log(N!) + N!) O(N!) is obtained.
Space Complexity
This method's temporal complexity is O(N! ), where N is the length of the supplied string.
Reason:
The recursive function use the O(N) recursion stack as the cause. Additionally, we keep the permutations in an O(N!)-sized list. The total space complexity is therefore O(N + N!) O(N!).
Drawbacks of the aforementioned strategy
The aforementioned strategy works well when each character in a string is unique. However, this method will display duplicate permutations if the string contains repeated characters, as you can see in the example above. To handle the aforementioned test scenario, there is a version of the backtracking strategy (discussed below).
Approach 2: Backtracking to Prevent Repetition
To accomplish this, we merely make a small change to the code above. We must make sure that no character is used twice for the prefix before calling the subproblems.
In essence, this means that only unique characters should be used at each level of the recursion. How then do we approach doing that?
To account for the characters utilized, we can achieve this by making a boolean array of size (26).
- If the character is not utilized, only then will the recursive function be called.
- A character can only be chosen once. The distinct selection criteria is therefore satisfied.
The permutations produced in the output will be in lexical order, which is a huge benefit of choosing the input string characters in this way (dictionary order). This makes verifying the program's accuracy simple.
Implementation of Approach-2
#include<bits/stdc++.h> |
Output :
aabc aacb |
Complexity in Time and Space
The above method has the same level of temporal complexity.
Have you already observed a difference in the outputs of two codes despite the identical input string "aac"?
Output 1 : displays repeated string permutations.
Output 2 :Yes, there are no repeating string variations.
Approach-3 :Utilizing the next permutation method of the C++ library.
The next permutation function is one of many available to change strings in the standard C++ library. If it is possible to rearrange the text in a more lexicographically extensive permutation, the next permutation function returns true. Otherwise, false is returned.
As a result, this approach likewise stays away from string repetitions.
Implementation of Approach-3
#include <bits/stdc++.h> |
Output :
aabc |
Time complexity
This method's time complexity is O(N * N!).
Reason:
The next permutation function's temporal complexity is O (N). N! calls are made to this procedure. The overall temporal complexity is therefore O(N * N!).
Space Complexity
This method has an O(N!) space complexity, where N is the length of the supplied string.
Reason:
This method uses no more space. It has continuous spatial complexity as a result.