打印一个字符串的所有排列,保留序列但改变大小写。
例子:
Input : ab
Output : AB Ab ab aB
Input : ABC
Output : abc Abc aBc ABc abC AbC aBC ABC
方法1(天真):天真的方法是遍历整个字符串,对于每个字符,请考虑两种情况:(1)改变大小写和重复(2)不要改变大小写和重复。
方法2(更好)对于长度为n的字符串,存在2 n个最大组合。我们可以将其表示为按位运算。
在“打印所有子序列”中讨论了相同的想法。
下面是上述想法的实现:
C++
// CPP code to print all permutations
// with respect to cases
#include
using namespace std;
// Function to generate permutations
void permute(string input)
{
int n = input.length();
// Number of permutations is 2^n
int max = 1 << n;
// Converting string to lower case
transform(input.begin(), input.end(), input.begin(),
::tolower);
// Using all subsequences and permuting them
for (int i = 0; i < max; i++) {
// If j-th bit is set, we convert it to upper case
string combination = input;
for (int j = 0; j < n; j++)
if (((i >> j) & 1) == 1)
combination[j] = toupper(input.at(j));
// Printing current combination
cout << combination << " ";
}
}
// Driver code
int main()
{
permute("ABC");
return 0;
}
Java
// Java program to print all permutations
// with respect to cases
public class PermuteString
{
// Function to generate permutations
static void permute(String input)
{
int n = input.length();
// Number of permutations is 2^n
int max = 1 << n;
// Converting string to lower case
input = input.toLowerCase();
// Using all subsequences and permuting them
for(int i = 0;i < max; i++)
{
char combination[] = input.toCharArray();
// If j-th bit is set, we convert it to upper case
for(int j = 0; j < n; j++)
{
if(((i >> j) & 1) == 1)
combination[j] = (char) (combination[j]-32);
}
// Printing current combination
System.out.print(combination);
System.out.print(" ");
}
}
// Driver Program to test above function
public static void main(String[] args)
{
permute("ABC");
}
}
// This code is contributed by Sumit Ghosh
Python
# Python code to print all permutations
# with respect to cases
# Function to generate permutations
def permute(inp):
n = len(inp)
# Number of permutations is 2^n
mx = 1 << n
# Converting string to lower case
inp = inp.lower()
# Using all subsequences and permuting them
for i in range(mx):
# If j-th bit is set, we convert it to upper case
combination = [k for k in inp]
for j in range(n):
if (((i >> j) & 1) == 1):
combination[j] = inp[j].upper()
temp = ""
# Printing current combination
for i in combination:
temp += i
print temp,
# Driver code
permute("ABC")
# This code is contributed by Sachin Bisht
C#
// C# program to print all permutations
// with respect to cases
using System;
class PermuteString {
// Function to generate
// permutations
static void permute(String input)
{
int n = input.Length;
// Number of permutations is 2^n
int max = 1 << n;
// Converting string
// to lower case
input = input.ToLower();
// Using all subsequences
// and permuting them
for(int i = 0;i < max; i++)
{
char []combination = input.ToCharArray();
// If j-th bit is set, we
// convert it to upper case
for(int j = 0; j < n; j++)
{
if(((i >> j) & 1) == 1)
combination[j] = (char) (combination[j] - 32);
}
// Printing current combination
Console.Write(combination);
Console.Write(" ");
}
}
// Driver Code
public static void Main()
{
permute("ABC");
}
}
// This code is contributed by Nitin Mittal.
PHP
> $j) & 1) == 1)
$combination[$j] = chr(ord($combination[$j]) - 32);
}
// Printing current combination
echo $combination . " ";
}
}
// Driver Code
permute("ABC");
// This code is contributed by mits
?>
输出:
abc Abc aBc ABc abC AbC aBC ABC
询问:Facebook。