Time complexity of all permutations of a string Last Updated : 19 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Time complexity of printing all permutations of a string. void perm(String str){ perm(str, ""); } void perm(String str, String prefix){ if(str.length() == 0){ System.out.println(prefix); } else{ for(int i = 0; i < str.length(); i++){ String rem = str.substring(0, i) + str.substring(i + 1); perm(rem, prefix + str.charAt(i)); } } } Understanding the Code: Line 1-3 : When call is to function perm passing "abc" as string argument, an internal call to perm(str, "") is made. This means that it is starting with an empty prefix (second argument) to create permutations for the entire string (first argument). Line 6-8 : This is the base case that will stop recursion. If there are no more characters left to be permuted in the input string, then print current permutation held in variable prefix and return. Line 9-12 : The for loop picks one character from input string at a time to update prefix string. That is, loop makes a call to function perm again with updated prefix and another string rem which collects the remaining characters of the input string. Understand this with the help of following image : Analyzing the Time Complexity : How many times does function perm get called in its base case? As we can understand from the recursion explained above that for a string of length 3 it is printing 6 permutations which is actually 3!. This is because if it needs to generate permutation, it is needed to pick characters for each slot. If there are 3 characters in our string, in the first slot, there are 3 choices, 2 choices for the next slot (for each of 3 choices earlier, i.e multiplication and not addition) and so on. This tells that there are n! permutations being printed in the base case which is what is shown in the image. How many times does function perm get called before its base case? Consider that lines 9 through 12 are hit n number of times. Therefore, there will be no more than (n * n!) function calls. How long does each function call take? Since, each character of string prefix needs to be printed, thus executing line 7 will take O(n) time. Line 10 and line 11 will also take O(n) time combined due to string concatenation, as sum of rem, prefix and str.charAt(i) will always be n. Each function call therefore corresponds to O(n) work. What is the total runtime?Calling perm O(n * n!) times (as an upper bound) and each call takes O(n) time, the total runtime will not exceed O(n!). Source: Cracking the Coding Interview by Gayle Laakmann McDowell. Comment More infoAdvertise with us Next Article Time complexity of all permutations of a string A AnureetKaur Follow Improve Article Tags : Misc Strings DSA Practice Tags : MiscStrings Similar Reads All permutations of a string using iteration A permutation, also called an âarrangement numberâ or âorderâ, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation ( Source: Mathword ) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB We hav 4 min read Lexicographically n-th permutation of a string Given a string of length m containing lowercase alphabets only. You have to find the n-th permutation of string lexicographically. Examples: Input : str[] = "abc", n = 3 Output : Result = "bac" Explanation : All possible permutation in sorted order: abc, acb, bac, bca, cab, cba Input : str[] = "aba" 6 min read Print all permutations of a string in Java Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For instance, the words âbatâ and âtabâ represents two distinct permutation (or arrangements) of a similar three lett 3 min read Print all Unique permutations of a given string. Given a string that may contain duplicates, the task is find all unique permutations of given string in any order.Examples: Input: "ABC"Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]Explanation: Given string ABC has 6 unique permutations as "ABC", "ACB", "BAC", "BCA", "CAB" and "CBA".Input: "AAA 12 min read Distinct permutations of the string | Set 2 Given a string s, the task is to return all unique permutations of a given string in lexicographically sorted order.Note: A permutation is the rearrangement of all the elements of a string. Examples:Input: s = "ABC"Output: "ABC", "ACB", "BAC", "BCA", "CBA", "CAB"Input: s = "XY"Output: "XY", "YX"Inpu 6 min read Like