PHP Program to print all permutations of a given string Last Updated : 10 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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(http://mathworld.wolfram.com/Permutation.html) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Here is a solution that is used as a basis in backtracking. PHP <?php // PHP program to print all // permutations of a given string. /* Permutation function @param str string to calculate permutation for @param l starting index @param r end index */ function permute($str, $l, $r) { if ($l == $r) echo $str. "\n"; else { for ($i = $l; $i <= $r; $i++) { $str = swap($str, $l, $i); permute($str, $l + 1, $r); $str = swap($str, $l, $i); } } } /* Swap Characters at position @param a string value @param i position 1 @param j position 2 @return swapped string */ function swap($a, $i, $j) { $temp; $charArray = str_split($a); $temp = $charArray[$i] ; $charArray[$i] = $charArray[$j]; $charArray[$j] = $temp; return implode($charArray); } // Driver Code $str = "ABC"; $n = strlen($str); permute($str, 0, $n - 1); // This code is contributed by mits. ?> Output: ABC ACB BAC BCA CBA CAB Algorithm Paradigm: Backtracking Time Complexity: O(n*n!) Note that there are n! permutations and it requires O(n) time to print a permutation. Auxiliary Space: O(r - l) Note: The above solution prints duplicate permutations if there are repeating characters in the input string. Please see the below link for a solution that prints only distinct permutations even if there are duplicates in input.Print all distinct permutations of a given string with duplicates. Permutations of a given string using STL Comment More infoAdvertise with us Next Article PHP Program to print all permutations of a given string K kartik Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-string Similar Reads PHP Program to Print All Duplicate Characters in a String This article will show you how to print duplicate characters in a string using PHP. Detecting and printing duplicate characters in a string is a common task in programming. In this article, we will explore various approaches to achieve this.Table of ContentUsing array_count_values() FunctionUsing As 3 min read PHP Program to Calculate the Permutation nPr This article will show you how to calculate the Permutation nPr using PHP. Permutations refer to the different ways in which a set of items can be arranged. In mathematics, the number of permutations of 'n' distinct items taken 'r' at a time is denoted as nPr. Calculating nPr, or permutations means 2 min read How to Get All Substrings of a Given String in PHP ? Extracting all possible substrings from a given string is a common task in text processing, pattern recognition, and various other applications in PHP programming. This operation involves generating every possible combination of characters present in the string, including both contiguous and non-con 3 min read PHP Program to Split & Join a String Given a String, the task is to split and join strings in PHP. It is a common operations that allow you to manipulate and manage text data effectively. Below are the approaches to split and join a string in PHP: Table of Content Using explode() and implode() FunctionUsing str_split() and Custom Join 3 min read How to generate a random, unique, alphanumeric string in PHP There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Table of ContentUsing str_shuffle() FunctionUsing md5() FunctionUsing sha1() FunctionUsing random_bytes() FunctionUsing random_int() in a Custom FunctionUsing uniqid() with more_entropy parameterUsing 4 min read Like