给定一个字符串,您需要打印所有可能的字符串,方法是在它们之间放置空格(零或一)。
Input: str[] = "ABC"
Output: ABC
AB C
A BC
A B C
资料来源:Amazon Interview Experience |设置158,第一轮,问题1。
这个想法是使用递归并创建一个缓冲区,该缓冲区一个接一个地包含所有带有空格的输出字符串。我们会在每个递归调用中不断更新缓冲区。如果给定字符串的长度为“ n”,则更新后的字符串的最大长度可以为n +(n-1),即2n-1。因此,我们创建了2n的缓冲区大小(用于字符串终止的一个额外字符)。
我们将第一个字符保持不变,从第二个字符,我们可以填充一个空格或一个字符。因此,可以编写如下的递归函数。
下面是上述方法的实现:
C++
// C++ program to print permutations
// of a given string with spaces.
#include
#include
using namespace std;
/* Function recursively prints
the strings having space pattern.
i and j are indices in 'str[]' and
'buff[]' respectively */
void printPatternUtil(const char str[],
char buff[], int i,
int j, int n)
{
if (i == n)
{
buff[j] = '\0';
cout << buff << endl;
return;
}
// Either put the character
buff[j] = str[i];
printPatternUtil(str, buff, i + 1, j + 1, n);
// Or put a space followed by next character
buff[j] = ' ';
buff[j + 1] = str[i];
printPatternUtil(str, buff, i + 1, j + 2, n);
}
// This function creates buf[] to
// store individual output string and uses
// printPatternUtil() to print all permutations.
void printPattern(const char* str)
{
int n = strlen(str);
// Buffer to hold the string
// containing spaces
// 2n - 1 characters and 1 string terminator
char buf[2 * n];
// Copy the first character as
// it is, since it will be always
// at first position
buf[0] = str[0];
printPatternUtil(str, buf, 1, 1, n);
}
// Driver program to test above functions
int main()
{
const char* str = "ABCD";
printPattern(str);
return 0;
}
Java
// Java program to print permutations
// of a given string with
// spaces
import java.io.*;
class Permutation
{
// Function recursively prints
// the strings having space
// pattern i and j are indices in 'String str' and
// 'buf[]' respectively
static void printPatternUtil(String str, char buf[],
int i, int j, int n)
{
if (i == n)
{
buf[j] = '\0';
System.out.println(buf);
return;
}
// Either put the character
buf[j] = str.charAt(i);
printPatternUtil(str, buf, i + 1,
j + 1, n);
// Or put a space followed
// by next character
buf[j] = ' ';
buf[j + 1] = str.charAt(i);
printPatternUtil(str, buf, i + 1,
j + 2, n);
}
// Function creates buf[] to
// store individual output
// string and uses printPatternUtil()
// to print all
// permutations
static void printPattern(String str)
{
int len = str.length();
// Buffer to hold the string
// containing spaces
// 2n-1 characters and 1
// string terminator
char[] buf = new char[2 * len];
// Copy the first character as it is, since it will
// be always at first position
buf[0] = str.charAt(0);
printPatternUtil(str, buf, 1, 1, len);
}
// Driver program
public static void main(String[] args)
{
String str = "ABCD";
printPattern(str);
}
}
Python
# Python program to print permutations
# of a given string with
# spaces.
# Utility function
def toString(List):
s = ""
for x in List:
if x == ' 092; 048;':
break
s += x
return s
# Function recursively prints the
# strings having space pattern.
# i and j are indices in 'str[]'
# and 'buff[]' respectively
def printPatternUtil(string, buff, i, j, n):
if i == n:
buff[j] = ' 092; 048;'
print toString(buff)
return
# Either put the character
buff[j] = string[i]
printPatternUtil(string, buff, i + 1,
j + 1, n)
# Or put a space followed by next character
buff[j] = ' '
buff[j + 1] = string[i]
printPatternUtil(string, buff, i + 1,
j + 2, n)
# This function creates buf[] to
# store individual output string
# and uses printPatternUtil() to
# print all permutations.
def printPattern(string):
n = len(string)
# Buffer to hold the string
# containing spaces
# 2n - 1 characters and 1 string terminator
buff = [0] * (2 * n)
# Copy the first character as it is,
# since it will be always
# at first position
buff[0] = string[0]
printPatternUtil(string, buff, 1, 1, n)
# Driver program
string = "ABCD"
printPattern(string)
# This code is contributed by BHAVYA JAIN
C#
// C# program to print permutations of a
// given string with spaces
using System;
class GFG
{
// Function recursively prints the
// strings having space pattern
// i and j are indices in 'String
// str' and 'buf[]' respectively
static void printPatternUtil(string str,
char[] buf, int i,
int j, int n)
{
if (i == n)
{
buf[j] = '\0';
Console.WriteLine(buf);
return;
}
// Either put the character
buf[j] = str[i];
printPatternUtil(str, buf, i + 1,
j + 1, n);
// Or put a space followed by next
// character
buf[j] = ' ';
buf[j + 1] = str[i];
printPatternUtil(str, buf, i + 1,
j + 2, n);
}
// Function creates buf[] to store
// individual output string and uses
// printPatternUtil() to print all
// permutations
static void printPattern(string str)
{
int len = str.Length;
// Buffer to hold the string containing
// spaces 2n-1 characters and 1 string
// terminator
char[] buf = new char[2 * len];
// Copy the first character as it is,
// since it will be always at first
// position
buf[0] = str[0];
printPatternUtil(str, buf, 1, 1, len);
}
// Driver program
public static void Main()
{
string str = "ABCD";
printPattern(str);
}
}
// This code is contributed by nitin mittal.
PHP
Java
// Java program for above approach
import java.util.*;
public class GFG
{
private static ArrayList
spaceString(String str)
{
ArrayList strs = new
ArrayList();
// Check if str.length() is 1
if (str.length() == 1)
{
strs.add(str);
return strs;
}
ArrayList strsTemp
= spaceString(str.substring(1,
str.length()));
// Iterate over strsTemp
for (int i = 0; i < strsTemp.size(); i++)
{
strs.add(str.charAt(0) +
strsTemp.get(i));
strs.add(str.charAt(0) + " " +
strsTemp.get(i));
}
// Return strs
return strs;
}
// Driver Code
public static void main(String args[])
{
ArrayList patterns
= new ArrayList();
// Function Call
patterns = spaceString("ABCD");
// Print patterns
for (String s : patterns)
{
System.out.println(s);
}
}
}
输出
ABCD
ABC D
AB CD
AB C D
A BCD
A BC D
A B CD
A B C D
时间复杂度:由于间隙的数量为n-1,因此共有2 ^(n-1)个图案,每个图案的长度范围为n到2n-1。因此,总体复杂度将为O(n *(2 ^ n))。
递归Java解决方案:
脚步:
1)取第一个字符,并在字符串的其余部分后面添加空格;
2)第一个字符+“空格” +间隔的字符串的其余字符;
2)第一个字符+其余字符串的其余字符;
Java
// Java program for above approach
import java.util.*;
public class GFG
{
private static ArrayList
spaceString(String str)
{
ArrayList strs = new
ArrayList();
// Check if str.length() is 1
if (str.length() == 1)
{
strs.add(str);
return strs;
}
ArrayList strsTemp
= spaceString(str.substring(1,
str.length()));
// Iterate over strsTemp
for (int i = 0; i < strsTemp.size(); i++)
{
strs.add(str.charAt(0) +
strsTemp.get(i));
strs.add(str.charAt(0) + " " +
strsTemp.get(i));
}
// Return strs
return strs;
}
// Driver Code
public static void main(String args[])
{
ArrayList patterns
= new ArrayList();
// Function Call
patterns = spaceString("ABCD");
// Print patterns
for (String s : patterns)
{
System.out.println(s);
}
}
}
输出
ABCD
A BCD
AB CD
A B CD
ABC D
A BC D
AB C D
A B C D