打印字符串的所有排列,保持顺序,但改变大小写。 例如:
null
Input : abOutput : AB Ab ab aBInput : ABCOutput : abc Abc aBc ABc abC AbC aBC ABC
方法1(天真): 幼稚的方法是遍历整个字符串,对于每一个字符,考虑两种情况,(1)改变情况和重复(2)不改变情况和重现。
C++
// CPP code to print all permutations // with respect to cases #include <bits/stdc++.h> using namespace std; // Function to generate permutations void permute(string ip, string op) { // base case if (ip.size() == 0){ cout<<op<< " " ; return ; } // pick lower and uppercase char ch = tolower (ip[0]) ; char ch2 = toupper (ip[0]) ; ip = ip.substr(1) ; permute(ip, op + ch ); permute(ip, op + ch2 ); } // Driver code int main() { string ip = "aB" ; permute(ip, "" ); return 0; } |
输出
ab aB Ab AB
注: 递归将仅按此顺序生成输出。
方法2(更好) 对于长度为n的字符串,存在2 N 最大组合。我们可以将其表示为按位操作。 同样的想法也在本文中讨论过 打印所有子序列 . 以下是上述理念的实施:
C++
// CPP code to print all permutations // with respect to cases #include <bits/stdc++.h> 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
<?php // PHP program to print all permutations // with respect to cases // Function to generate permutations function permute( $input ) { $n = strlen ( $input ); // Number of permutations is 2^n $max = 1 << $n ; // Converting string to lower case $input = strtolower ( $input ); // Using all subsequences and permuting them for ( $i = 0; $i < $max ; $i ++) { $combination = $input ; // If j-th bit is set, we convert // it to upper case for ( $j = 0; $j < $n ; $j ++) { if ((( $i >> $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 ?> |
Javascript
<script> // javascript program to print all permutations // with respect to cases // Function to generate permutations function permute(input) { var n = input.length; // Number of permutations is 2^n var max = 1 << n; // Converting string to lower case input = input.toLowerCase(); // Using all subsequences and permuting them for ( var i = 0;i < max; i++) { var combination = input.split( '' ); // If j-th bit is set, we convert it to upper case for ( var j = 0; j < n; j++) { if (((i >> j) & 1) == 1) combination[j] = String.fromCharCode(combination[j].charCodeAt(0)-32); } // Printing current combination document.write(combination.join( '' )); document.write( " " ); } } // Driver Program to test above function permute( "ABC" ); // This code contributed by Princi Singh </script> |
输出
abc Abc aBc ABc abC AbC aBC ABC
被问到: 脸谱网 . 本文由 罗希特·塔普里亚尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END