Generate all possible permutations of words in a String
Permutations refer to the different possible arrangements or orderings of a set of items. In this case, we are interested in finding all possible permutations of the words in a given string.
Permutation is a mathematical concept of arranging objects in different orders. In the context of a string, it means rearranging the words in different orders. For example, the string "Win World Cup 2021" can be rearranged into "Win Cup World 2021", "World Cup Win 2021", "2021 Win World Cup", and so on.
To generate all possible permutations of words in a string, we can use the following algorithm:
- Split the string into words.
- Generate all possible combinations of words.
- For each combination, generate all possible permutations of words.
Code Solution
/*
Java Program for
Generate all possible permutations of words in a String
*/
public class Permutations
{
// Swap the two elements in given array of words
public void swap(String[] words, int a, int b)
{
String temp = words[a];
words[a] = words[b];
words[b] = temp;
}
public void display(String[] words, int n, int index)
{
if (index == n)
{
// Display calculated result
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
System.out.print(" ");
}
System.out.print(words[i]);
}
// Include new line
System.out.print("\n");
return;
}
for (int i = index; i < n; ++i)
{
// Swap the element value
swap(words, i, index);
// Find the next permutation of recursively
display(words, n, index + 1);
// Swap the element value
swap(words, i, index);
}
}
public void wordPermutation(String text)
{
int n = text.length();
if (n == 0)
{
return;
}
// Spit text by space
String[] words = text.split(" ");
// Get the number of words
n = words.length;
// Print permutation
display(words, n, 0);
}
public static void main(String[] args)
{
Permutations task = new Permutations();
String text = "Win World Cup 2021";
// Test
task.wordPermutation(text);
}
}
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
// Include namespace system
using System;
/*
Csharp Program for
Generate all possible permutations of words in a String
*/
public class Permutations
{
// Swap the two elements in given array of words
public void swap(String[] words, int a, int b)
{
String temp = words[a];
words[a] = words[b];
words[b] = temp;
}
public void display(String[] words, int n, int index)
{
if (index == n)
{
// Display calculated result
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
Console.Write(" ");
}
Console.Write(words[i]);
}
// Include new line
Console.Write("\n");
return;
}
for (int i = index; i < n; ++i)
{
// Swap the element value
this.swap(words, i, index);
// Find the next permutation of recursively
this.display(words, n, index + 1);
// Swap the element value
this.swap(words, i, index);
}
}
public void wordPermutation(String text)
{
int n = text.Length;
if (n == 0)
{
return;
}
// Spit text by space
String[] words = text.Split(" ");
// Get the number of words
n = words.Length;
// Print permutation
this.display(words, n, 0);
}
public static void Main(String[] args)
{
Permutations task = new Permutations();
String text = "Win World Cup 2021";
// Test
task.wordPermutation(text);
}
}
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
package main
import "strings"
import "fmt"
/*
Go Program for
Generate all possible permutations of words in a String
*/
type Permutations struct {}
func getPermutations() * Permutations {
var me *Permutations = &Permutations {}
return me
}
// Swap the two elements in given array of words
func(this Permutations) swap(words[] string,
a int, b int) {
var temp string = words[a]
words[a] = words[b]
words[b] = temp
}
func(this Permutations) display(words[] string,
n int, index int) {
if index == n {
// Display calculated result
for i := 0 ; i < n ; i++ {
if i != 0 {
fmt.Print(" ")
}
fmt.Print(words[i])
}
// Include new line
fmt.Print("\n")
return
}
for i := index ; i < n ; i++ {
// Swap the element value
this.swap(words, i, index)
// Find the next permutation of recursively
this.display(words, n, index + 1)
// Swap the element value
this.swap(words, i, index)
}
}
func(this Permutations) wordPermutation(text string) {
var n int = len(text)
if n == 0 {
return
}
// Spit text by space
var words = strings.Split(text, " ")
// Get the number of words
n = len(words)
// Print permutation
this.display(words, n, 0)
}
func main() {
var task * Permutations = getPermutations()
var text string = "Win World Cup 2021"
// Test
task.wordPermutation(text)
}
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
<?php
/*
Php Program for
Generate all possible permutations of words in a String
*/
class Permutations
{
// Swap the two elements in given array of words
public function swap(&$words, $a, $b)
{
$temp = $words[$a];
$words[$a] = $words[$b];
$words[$b] = $temp;
}
public function display(&$words, $n, $index)
{
if ($index == $n)
{
// Display calculated result
for ($i = 0; $i < $n; ++$i)
{
if ($i != 0)
{
echo(" ");
}
echo($words[$i]);
}
// Include new line
echo("\n");
return;
}
for ($i = $index; $i < $n; ++$i)
{
// Swap the element value
$this->swap($words, $i, $index);
// Find the next permutation of recursively
$this->display($words, $n, $index + 1);
// Swap the element value
$this->swap($words, $i, $index);
}
}
public function wordPermutation($text)
{
$n = strlen($text);
if ($n == 0)
{
return;
}
// Spit text by space
$words = explode(" ", $text);
// Get the number of words
$n = count($words);
// Print permutation
$this->display($words, $n, 0);
}
}
function main()
{
$task = new Permutations();
$text = "Win World Cup 2021";
// Test
$task->wordPermutation($text);
}
main();
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
/*
Node JS Program for
Generate all possible permutations of words in a String
*/
class Permutations
{
// Swap the two elements in given array of words
swap(words, a, b)
{
var temp = words[a];
words[a] = words[b];
words[b] = temp;
}
display(words, n, index)
{
if (index == n)
{
// Display calculated result
for (var i = 0; i < n; ++i)
{
if (i != 0)
{
process.stdout.write(" ");
}
process.stdout.write(words[i]);
}
// Include new line
process.stdout.write("\n");
return;
}
for (var i = index; i < n; ++i)
{
// Swap the element value
this.swap(words, i, index);
// Find the next permutation of recursively
this.display(words, n, index + 1);
// Swap the element value
this.swap(words, i, index);
}
}
wordPermutation(text)
{
var n = text.length;
if (n == 0)
{
return;
}
// Spit text by space
var words = text.split(" ");
// Get the number of words
n = words.length;
// Print permutation
this.display(words, n, 0);
}
}
function main()
{
var task = new Permutations();
var text = "Win World Cup 2021";
// Test
task.wordPermutation(text);
}
main();
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
# Python 3 Program for
# Generate all possible permutations of words in a String
class Permutations :
# Swap the two elements in given list of words
def swap(self, words, a, b) :
temp = words[a]
words[a] = words[b]
words[b] = temp
def display(self, words, n, index) :
if (index == n) :
i = 0
# Display calculated result
while (i < n) :
if (i != 0) :
print(" ", end = "")
print(words[i], end = "")
i += 1
# Include new line
print(end = "\n")
return
i = index
while (i < n) :
# Swap the element value
self.swap(words, i, index)
# Find the next permutation of recursively
self.display(words, n, index + 1)
# Swap the element value
self.swap(words, i, index)
i += 1
def wordPermutation(self, text) :
n = len(text)
if (n == 0) :
return
# Spit text by space
words = text.split(" ")
# Get the number of words
n = len(words)
# Print permutation
self.display(words, n, 0)
def main() :
task = Permutations()
text = "Win World Cup 2021"
# Test
task.wordPermutation(text)
if __name__ == "__main__": main()
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
# Ruby Program for
# Generate all possible permutations of words in a String
class Permutations
# Swap the two elements in given array of words
def swap(words, a, b)
temp = words[a]
words[a] = words[b]
words[b] = temp
end
def display(words, n, index)
if (index == n)
i = 0
# Display calculated result
while (i < n)
if (i != 0)
print(" ")
end
print(words[i])
i += 1
end
# Include new line
print("\n")
return
end
i = index
while (i < n)
# Swap the element value
self.swap(words, i, index)
# Find the next permutation of recursively
self.display(words, n, index + 1)
# Swap the element value
self.swap(words, i, index)
i += 1
end
end
def wordPermutation(text)
n = text.length
if (n == 0)
return
end
# Spit text by space
words = text.split(" ")
# Get the number of words
n = words.length
# Print permutation
self.display(words, n, 0)
end
end
def main()
task = Permutations.new()
text = "Win World Cup 2021"
# Test
task.wordPermutation(text)
end
main()
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
import scala.collection.mutable._;
/*
Scala Program for
Generate all possible permutations of words in a String
*/
class Permutations()
{
// Swap the two elements in given array of words
def swap(words: Array[String], a: Int, b: Int): Unit = {
var temp: String = words(a);
words(a) = words(b);
words(b) = temp;
}
def display(words: Array[String], n: Int, index: Int): Unit = {
if (index == n)
{
var i: Int = 0;
// Display calculated result
while (i < n)
{
if (i != 0)
{
print(" ");
}
print(words(i));
i += 1;
}
// Include new line
print("\n");
return;
}
var i: Int = index;
while (i < n)
{
// Swap the element value
swap(words, i, index);
// Find the next permutation of recursively
display(words, n, index + 1);
// Swap the element value
swap(words, i, index);
i += 1;
}
}
def wordPermutation(text: String): Unit = {
var n: Int = text.length();
if (n == 0)
{
return;
}
// Spit text by space
var words: Array[String] = text.split(" ");
// Get the number of words
n = words.length;
// Print permutation
display(words, n, 0);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Permutations = new Permutations();
var text: String = "Win World Cup 2021";
// Test
task.wordPermutation(text);
}
}
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
import Foundation;
/*
Swift 4 Program for
Generate all possible permutations of words in a String
*/
class Permutations
{
// Swap the two elements in given array of words
func swap(_ words: inout[String], _ a: Int, _ b: Int)
{
let temp: String = words[a];
words[a] = words[b];
words[b] = temp;
}
func display(_ words: inout[String], _ n: Int, _ index: Int)
{
if (index == n)
{
var i: Int = 0;
// Display calculated result
while (i < n)
{
if (i != 0)
{
print(" ", terminator: "");
}
print(words[i], terminator: "");
i += 1;
}
// Include new line
print(terminator: "\n");
return;
}
var i: Int = index;
while (i < n)
{
// Swap the element value
self.swap(&words, i, index);
// Find the next permutation of recursively
self.display(&words, n, index + 1);
// Swap the element value
self.swap(&words, i, index);
i += 1;
}
}
func wordPermutation(_ text: String)
{
var n: Int = text.count;
if (n == 0)
{
return;
}
// Spit text by space
var words: [String] = text.split{$0 == " "}.map(String.init);
// Get the number of words
n = words.count;
// Print permutation
self.display(&words, n, 0);
}
}
func main()
{
let task: Permutations = Permutations();
let text: String = "Win World Cup 2021";
// Test
task.wordPermutation(text);
}
main();
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
/*
Kotlin Program for
Generate all possible permutations of words in a String
*/
class Permutations
{
// Swap the two elements in given array of words
fun swap(words: Array < String > , a: Int, b: Int): Unit
{
val temp: String = words[a];
words[a] = words[b];
words[b] = temp;
}
fun display(words: Array < String > , n: Int, index: Int): Unit
{
if (index == n)
{
var i: Int = 0;
// Display calculated result
while (i < n)
{
if (i != 0)
{
print(" ");
}
print(words[i]);
i += 1;
}
// Include new line
print("\n");
return;
}
var i: Int = index;
while (i < n)
{
// Swap the element value
this.swap(words, i, index);
// Find the next permutation of recursively
this.display(words, n, index + 1);
// Swap the element value
this.swap(words, i, index);
i += 1;
}
}
fun wordPermutation(text: String): Unit
{
var n: Int = text.length;
if (n == 0)
{
return;
}
// Spit text by space
val words: Array < String > = text.split(" ").toTypedArray();
// Get the number of words
n = words.count();
// Print permutation
this.display(words, n, 0);
}
}
fun main(args: Array < String > ): Unit
{
val task: Permutations = Permutations();
val text: String = "Win World Cup 2021";
// Test
task.wordPermutation(text);
}
Output
Win World Cup 2021
Win World 2021 Cup
Win Cup World 2021
Win Cup 2021 World
Win 2021 Cup World
Win 2021 World Cup
World Win Cup 2021
World Win 2021 Cup
World Cup Win 2021
World Cup 2021 Win
World 2021 Cup Win
World 2021 Win Cup
Cup World Win 2021
Cup World 2021 Win
Cup Win World 2021
Cup Win 2021 World
Cup 2021 Win World
Cup 2021 World Win
2021 World Cup Win
2021 World Win Cup
2021 Cup World Win
2021 Cup Win World
2021 Win Cup World
2021 Win World Cup
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment