PHP Program to print all permutations of a given string Last Updated : 10 Dec, 2021 Comments Improve Suggest changes 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 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 PHP Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1.Examples: Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2.Input: s1 = "practice", s2 = "geeksforgeeks"Output: -1.Ex 2 min read PHP Program to Find Missing Characters to Make a String Pangram In this article, we will see how to find the missing characters to make the string Pangram using PHP. A pangram is a sentence containing every letter in the English Alphabet. Examples: Input: The quick brown fox jumps over the dogOutput: Missing Characters - alyzInput: The quick brown fox jumpsOutpu 2 min read Split a String into an Array of Words in PHP In PHP, splitting a string into an array involves breaking a string into smaller parts based on a delimiter or a pattern and storing those parts as elements of an array. PHP provides several methods to achieve this, making it easy to convert a string into an array of words:Table of ContentUsing expl 3 min read How to Print Unique Elements an Given Array in PHP? Given an array, the task is to print all distinct elements of the given array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.Examples: Input: arr[] = {5, 6, 5, 3, 6, 4, 3, 5}Output: 5, 6, 3, 4Input: arr[] = {1, 2, 3, 4, 5, 5 2 min read Reversing the Even Length Words of a String in PHP Reversing the even-length words of a string is a common programming task that involves manipulating individual words within a sentence. In PHP, there are several approaches to achieve this, each offering different levels of complexity and flexibility.Table of ContentUsing Explode and Implode Functio 3 min read Like