Posted on by Kalkicode
Code Backtracking

Permutation with spaces

All the possible ways to arrange the characters of a given string such that spaces are also included as part of the arrangement.

To illustrate, let's take the word "hello" as an example. The possible permutations with spaces of this word would be:

• "h e l l o"
• "h e llo"
• "h el l o"
• "h ello"
• "he l l o"
• "he llo"
• "hel l o"
• "hello"

Note that each of these permutations includes spaces between the characters. Also, some permutations may have more than one space between the characters.

The number of possible permutations with spaces for a given string depends on the length of the string. For a string of length n, there are (n-1)! possible permutations with spaces. This is because there are (n-1) spaces between the n characters, and we can choose to place a space or not place a space in each of these (n-1) positions, giving us (n-1) binary choices. The number of possible permutations with spaces is therefore equal to 2^(n-1), and since each permutation corresponds to a unique binary string of length (n-1), the total number of permutations with spaces is equal to (n-1)!

Program Solution

``````// C program
// Permutation with spaces
#include <stdio.h>
#include <string.h>

// Perform of permutation with include space between characters
void spacePermutation(const char *str, char result[], int n, int i, int j)
{
if (i == n)
{
// Display permutation
for (int k = 0; k < n *2; ++k)
{
printf("%c", result[k]);
}
// Include new line
printf("\n");
return;
}
// Include str element
result[j] = str[i];
// Find next permutation Through by recursion
spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result[j] = ' ';
// Include str element
result[j + 1] = str[i];
// Find next permutation Through by recursion
spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
void permutation(const char *str)
{
// Get the size
int n = strlen(str);
// This is used to store result
char result[n *2];
// Set initial value
for (int i = 0; i < n *2; ++i)
{
if (i < n)
{
result[i] = str[i];
}
else
{
// Append the space character at last
result[i] = ' ';
}
}
spacePermutation(str, result, n, 1, 1);
}
int main()
{
const char *str = "SETTING";
permutation(str);
return 0;
}``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````/*
Java Program for
Permutation with spaces
*/
class Permutations
{
// Perform of permutation with include space between characters
public void spacePermutation(String str, char[] result, int n, int i, int j)
{
if (i == n)
{
// Display permutation
for (int k = 0; k < n * 2; ++k)
{
System.out.print(result[k]);
}
// Include new line
System.out.print("\n");
return;
}
// Include str element
result[j] = str.charAt(i);
// Find next permutation Through by recursion
spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result[j] = ' ';
// Include str element
result[j + 1] = str.charAt(i);
// Find next permutation Through by recursion
spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
public void permutation(String str)
{
// Get the size
int n = str.length();
// This is used to store result
char[] result = new char[n * 2];
// Set initial value
for (int i = 0; i < n * 2; ++i)
{
if (i < n)
{
result[i] = str.charAt(i);
}
else
{
// Append the space character at last
result[i] = ' ';
}
}
spacePermutation(str, result, n, 1, 1);
}
public static void main(String[] args)
{
String str = "SETTING";
}
}``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````// Include header file
#include <iostream>
#include<string.h>

using namespace std;
/*
C++ Program for
Permutation with spaces
*/
class Permutations
{
public:
// Perform of permutation with include space between characters
void spacePermutation(string str, char result[], int n, int i, int j)
{
if (i == n)
{
// Display permutation
for (int k = 0; k < n *2; ++k)
{
cout << result[k];
}
// Include new line
cout << "\n";
return;
}
// Include str element
result[j] = str[i];
// Find next permutation Through by recursion
this->spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result[j] = ' ';
// Include str element
result[j + 1] = str[i];
// Find next permutation Through by recursion
this->spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
void permutation(string str)
{
// Get the size
int n = str.size();
// This is used to store result
char result[n *2];
// Set initial value
for (int i = 0; i < n *2; ++i)
{
if (i < n)
{
result[i] = str[i];
}
else
{
// Append the space character at last
result[i] = ' ';
}
}
this->spacePermutation(str, result, n, 1, 1);
}
};
int main()
{
string str = "SETTING";
return 0;
}``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````// Include namespace system
using System;
/*
C# Program for
Permutation with spaces
*/
public class Permutations
{
// Perform of permutation with include space between characters
public void spacePermutation(String str, char[] result, int n, int i, int j)
{
if (i == n)
{
// Display permutation
for (int k = 0; k < n * 2; ++k)
{
Console.Write(result[k]);
}
// Include new line
Console.Write("\n");
return;
}
// Include str element
result[j] = str[i];
// Find next permutation Through by recursion
spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result[j] = ' ';
// Include str element
result[j + 1] = str[i];
// Find next permutation Through by recursion
spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
public void permutation(String str)
{
// Get the size
int n = str.Length;
// This is used to store result
char[] result = new char[n * 2];
// Set initial value
for (int i = 0; i < n * 2; ++i)
{
if (i < n)
{
result[i] = str[i];
}
else
{
// Append the space character at last
result[i] = ' ';
}
}
spacePermutation(str, result, n, 1, 1);
}
public static void Main(String[] args)
{
String str = "SETTING";
}
}``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````<?php
/*
Php Program for
Permutation with spaces
*/
class Permutations
{
// Perform of permutation with include space between characters
public	function spacePermutation(\$str, & \$result, \$n, \$i, \$j)
{
if (\$i == \$n)
{
// Display permutation
for (\$k = 0; \$k < \$n * 2; ++\$k)
{
echo \$result[\$k];
}
// Include new line
echo "\n";
return;
}
// Include str element
\$result[\$j] = \$str[\$i];
// Find next permutation Through by recursion
\$this->spacePermutation(\$str, \$result, \$n, \$i + 1, \$j + 1);
// Include space
\$result[\$j] = ' ';
// Include str element
\$result[\$j + 1] = \$str[\$i];
// Find next permutation Through by recursion
\$this->spacePermutation(\$str, \$result, \$n, \$i + 1, \$j + 2);
}
// Handles the request of finding permutation
public	function permutation(\$str)
{
// Get the size
\$n = strlen(\$str);
// This is used to store result
\$result = array_fill(0, \$n * 2, ' ');
// Set initial value
for (\$i = 0; \$i < \$n; ++\$i)
{
if (\$i < \$n)
{
\$result[\$i] = \$str[\$i];
}
}
\$this->spacePermutation(\$str, \$result, \$n, 1, 1);
}
}

function main()
{
\$str = "SETTING";
}
main();``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````/*
Node Js Program for
Permutation with spaces
*/
class Permutations
{
// Perform of permutation with include space between characters
spacePermutation(str, result, n, i, j)
{
if (i == n)
{
// Display permutation
for (var k = 0; k < n * 2; ++k)
{
process.stdout.write(result[k]);
}
// Include new line
process.stdout.write("\n");
return;
}
// Include str element
result[j] = str[i];
// Find next permutation Through by recursion
this.spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result[j] = ' ';
// Include str element
result[j + 1] = str[i];
// Find next permutation Through by recursion
this.spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
permutation(str)
{
// Get the size
var n = str.length;
// This is used to store result
var result = Array(n * 2).fill(' ');
// Set initial value
for (var i = 0; i < n; ++i)
{
if (i < n)
{
result[i] = str[i];
}
}
this.spacePermutation(str, result, n, 1, 1);
}
}

function main()
{
var str = "SETTING";
}
main();``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````#   Python 3 Program for
#   Permutation with spaces

class Permutations :
#  Perform of permutation with include space between characters
def spacePermutation(self, str, result, n, i, j) :
if (i == n) :
#  Display permutation
k = 0
while (k < n * 2) :
print(result[k], end = "")
k += 1

#  Include new line
print(end = "\n")
return

#  Include str element
result[j] = str[i]
#  Find next permutation Through by recursion
self.spacePermutation(str, result, n, i + 1, j + 1)
#  Include space
result[j] = ' '
#  Include str element
result[j + 1] = str[i]
#  Find next permutation Through by recursion
self.spacePermutation(str, result, n, i + 1, j + 2)

#  Handles the request of finding permutation
def permutation(self, str) :
#  Get the size
n = len(str)
#  This is used to store result
result = [ ' '
] * (n * 2)
#  Set initial value
i = 0
while (i < n) :
if (i < n) :
result[i] = str[i]
i += 1
self.spacePermutation(str, result, n, 1, 1)

def main() :
str = "SETTING"

if __name__ == "__main__": main()``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````#   Ruby Program for
#   Permutation with spaces

class Permutations
#  Perform of permutation with include space between characters
def spacePermutation(str, result, n, i, j)
if (i == n)
#  Display permutation
k = 0
while (k < n * 2)
print(result[k])
k += 1
end
#  Include new line
print("\n")
return
end

#  Include str element
result[j] = str[i]
#  Find next permutation Through by recursion
self.spacePermutation(str, result, n, i + 1, j + 1)
#  Include space
result[j] = ' '
#  Include str element
result[j + 1] = str[i]
#  Find next permutation Through by recursion
self.spacePermutation(str, result, n, i + 1, j + 2)
end

#  Handles the request of finding permutation
def permutation(str)
#  Get the size
n = str.length()
#  This is used to store result
result = Array.new(n * 2) {' '}
#  Set initial value
i = 0
while (i < n)
if (i < n)
result[i] = str[i]
end
i += 1
end

self.spacePermutation(str, result, n, 1, 1)
end

end

def main()
str = "SETTING"
end

main()``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G
``````
``````/*
Scala Program for
Permutation with spaces
*/
class Permutations
{
// Perform of permutation with include space between characters
def spacePermutation(str: String, result: Array[Character], n: Int, i: Int, j: Int): Unit = {
if (i == n)
{
// Display permutation
var k: Int = 0;
while (k < n * 2)
{
print(result(k));
k += 1;
}
// Include new line
print("\n");
return;
}
// Include str element
result(j) = str(i);
// Find next permutation Through by recursion
this.spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result(j) = ' ';
// Include str element
result(j + 1) = str(i);
// Find next permutation Through by recursion
this.spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
def permutation(str: String): Unit = {
// Get the size
var n: Int = str.length();
// This is used to store result
var result: Array[Character] = Array.fill[Character](n * 2)(' ');
// Set initial value
var i: Int = 0;
while (i < n)
{
if (i < n)
{
result(i) = str(i);
}
i += 1;
}
this.spacePermutation(str, result, n, 1, 1);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Permutations = new Permutations();
var str: String = "SETTING";
}
}``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````/*
Swift 4 Program for
Permutation with spaces
*/
class Permutations
{
// Perform of permutation with include space between characters
func spacePermutation( _ str:[Character], _ result: inout[Character], _ n: Int, _ i: Int, _ j: Int)
{
if (i == n)
{
// Display permutation
var k: Int = 0;
while (k < n * 2)
{
print(result[k], terminator: "");
k += 1;
}
// Include new line
print(terminator: "\n");
return;
}
// Include str element
result[j] = str[i];
// Find next permutation Through by recursion
self.spacePermutation(str, &result, n, i + 1, j + 1);
// Include space
result[j] = " ";
// Include str element
result[j + 1] = str[i];
// Find next permutation Through by recursion
self.spacePermutation(str, &result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
func permutation(_ text: String)
{
// Get the size
let n: Int = text.count;
// This is used to store result
var result: [Character] = Array(repeating: " ", count: n * 2);
let str = Array(text);
// Set initial value
var i: Int = 0;
while (i < n)
{
if (i < n)
{
result[i] = str[i];
}
i += 1;
}
self.spacePermutation(str, &result, n, 1, 1);
}
}
func main()
{
let str: String = "SETTING";
}
main();``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````
``````/*
Kotlin Program for
Permutation with spaces
*/
class Permutations
{
// Perform of permutation with include space between characters
fun spacePermutation(str: String, result: Array<Char> , n: Int, i: Int, j: Int): Unit
{
if (i == n)
{
// Display permutation
var k: Int = 0;
while (k < n * 2)
{
print(result[k]);
k += 1;
}
// Include new line
print("\n");
return;
}
// Include str element
result[j] = str[i];
// Find next permutation Through by recursion
this.spacePermutation(str, result, n, i + 1, j + 1);
// Include space
result[j] = ' ';
// Include str element
result[j + 1] = str[i];
// Find next permutation Through by recursion
this.spacePermutation(str, result, n, i + 1, j + 2);
}
// Handles the request of finding permutation
fun permutation(str: String): Unit
{
// Get the size
var n: Int = str.count();
// This is used to store result
var result: Array<Char> = Array(n * 2){' '};
// Set initial value
var i: Int = 0;
while (i < n)
{
if (i < n)
{
result[i] = str[i];
}
i += 1;
}
this.spacePermutation(str, result, n, 1, 1);
}
}
fun main(args: Array<String> ): Unit
{
var str: String = "SETTING";
}``````

Output

``````SETTING
SETTIN G
SETTI NG
SETTI N G
SETT INGG
SETT IN G
SETT I NG
SETT I N G
SET TING G
SET TIN GG
SET TI NGG
SET TI N G
SET T INGG
SET T IN G
SET T I NG
SET T I N G
SE TTINGN G
SE TTIN G G
SE TTI NG G
SE TTI N GG
SE TT INGGG
SE TT IN GG
SE TT I NGG
SE TT I N G
SE T TING G
SE T TIN GG
SE T TI NGG
SE T TI N G
SE T T INGG
SE T T IN G
SE T T I NG
SE T T I N G
S ETTING N G
S ETTIN GN G
S ETTI NGN G
S ETTI N G G
S ETT INGG G
S ETT IN G G
S ETT I NG G
S ETT I N GG
S ET TING GG
S ET TIN GGG
S ET TI NGGG
S ET TI N GG
S ET T INGGG
S ET T IN GG
S ET T I NGG
S ET T I N G
S E TTINGN G
S E TTIN G G
S E TTI NG G
S E TTI N GG
S E TT INGGG
S E TT IN GG
S E TT I NGG
S E TT I N G
S E T TING G
S E T TIN GG
S E T TI NGG
S E T TI N G
S E T T INGG
S E T T IN G
S E T T I NG
S E T T I N G``````

Comment

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.

Categories
Relative Post