Print all permutations of a string keeping the sequence but changing cases.
Examples:
Input : ab
Output : AB Ab ab aB
Input : ABC
Output : abc Abc aBc ABc abC AbC aBC ABC
Method 1 (Naive) : Naive approach would be to traverse the whole string and for every character, consider two cases, (1) change case and recur (2) Do not change case and recur.
C++
#include <bits/stdc++.h>
using namespace std;
void permute(string ip, string op)
{
if (ip.size() == 0) {
cout << op << " " ;
return ;
}
char ch = tolower (ip[0]);
char ch2 = toupper (ip[0]);
ip = ip.substr(1);
permute(ip, op + ch);
permute(ip, op + ch2);
}
int main()
{
string ip = "aB" ;
permute(ip, "" );
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void permute(String ip, String op)
{
if (ip.length() == 0 ){
System.out.print(op + " " );
return ;
}
String ch = ( "" + ip.charAt( 0 )).toLowerCase();
String ch2 = ( "" + ip.charAt( 0 )).toUpperCase();
ip = ip.substring( 1 , ip.length()) ;
permute(ip, op + ch);
permute(ip, op + ch2);
}
public static void main(String[] args)
{
String ip = "aB" ;
permute(ip, "" );
}
}
|
Python3
def permute(ip, op):
if len (ip) = = 0 :
print (op, end = " " )
return
ch = ip[ 0 ].lower()
ch2 = ip[ 0 ].upper()
ip = ip[ 1 :]
permute(ip, op + ch)
permute(ip, op + ch2)
def main():
ip = "AB"
permute(ip, "")
main()
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void permute( string ip, string op)
{
if (ip.Length == 0){
Console.Write(op + " " );
return ;
}
string ch = ( "" + ip[0]).ToLower();
string ch2 = ( "" + ip[0]).ToUpper();
ip = ip.Substring(1) ;
permute(ip, op + ch);
permute(ip, op + ch2);
}
public static void Main( string [] args)
{
string ip = "aB" ;
permute(ip, "" );
}
}
|
Javascript
<script>
function permute(ip, op)
{
if (ip.length == 0){
document.write(op, " " );
return ;
}
let ch = ip[0].toLowerCase();
let ch2 = ip[0].toUpperCase();
ip = ip.substring(1) ;
permute(ip, op + ch);
permute(ip, op + ch2);
}
let ip = "aB" ;
permute(ip, "" );
</script>
|
Note: Recursion will generate output in this order only.
Time Complexity: O(n*2n))
Auxiliary Space: O(n)
Method 2: For a string of length n there exist 2n maximum combinations. We can represent this as a bitwise operation.
The same idea is discussed in Print all subsequences.
Below is the implementation of the above idea :
C++
#include <bits/stdc++.h>
using namespace std;
void permute(string input)
{
int n = input.length();
int max = 1 << n;
transform(input.begin(), input.end(), input.begin(),
:: tolower );
for ( int i = 0; i < max; i++) {
string combination = input;
for ( int j = 0; j < n; j++)
if (((i >> j) & 1) == 1)
combination[j] = toupper (input.at(j));
cout << combination << " " ;
}
}
int main()
{
permute( "ABC" );
return 0;
}
|
Java
public class PermuteString {
static void permute(String input)
{
int n = input.length();
int max = 1 << n;
input = input.toLowerCase();
for ( int i = 0 ; i < max; i++) {
char combination[] = input.toCharArray();
for ( int j = 0 ; j < n; j++) {
if (((i >> j) & 1 ) == 1 )
combination[j]
= ( char )(combination[j] - 32 );
}
System.out.print(combination);
System.out.print( " " );
}
}
public static void main(String[] args)
{
permute( "ABC" );
}
}
|
Python
def permute(inp):
n = len (inp)
mx = 1 << n
inp = inp.lower()
for i in range (mx):
combination = [k for k in inp]
for j in range (n):
if (((i >> j) & 1 ) = = 1 ):
combination[j] = inp[j].upper()
temp = ""
for i in combination:
temp + = i
print temp,
permute( "ABC" )
|
C#
using System;
class PermuteString {
static void permute(String input)
{
int n = input.Length;
int max = 1 << n;
input = input.ToLower();
for ( int i = 0; i < max; i++) {
char [] combination = input.ToCharArray();
for ( int j = 0; j < n; j++) {
if (((i >> j) & 1) == 1)
combination[j]
= ( char )(combination[j] - 32);
}
Console.Write(combination);
Console.Write( " " );
}
}
public static void Main() { permute( "ABC" ); }
}
|
PHP
<?php
function permute( $input )
{
$n = strlen ( $input );
$max = 1 << $n ;
$input = strtolower ( $input );
for ( $i = 0; $i < $max ; $i ++)
{
$combination = $input ;
for ( $j = 0; $j < $n ; $j ++)
{
if ((( $i >> $j ) & 1) == 1)
$combination [ $j ] = chr (ord( $combination [ $j ]) - 32);
}
echo $combination . " " ;
}
}
permute( "ABC" );
?>
|
Javascript
<script>
function permute(input)
{
var n = input.length;
var max = 1 << n;
input = input.toLowerCase();
for ( var i = 0;i < max; i++)
{
var combination = input.split( '' );
for ( var j = 0; j < n; j++)
{
if (((i >> j) & 1) == 1)
combination[j] = String.fromCharCode(combination[j].charCodeAt(0)-32);
}
document.write(combination.join( '' ));
document.write( " " );
}
}
permute( "ABC" );
</script>
|
Output
abc Abc aBc ABc abC AbC aBC ABC
Time complexity: O(n*2n) , since we are running a nested loop of size n inside a loop of size 2n.
Auxiliary Space: O(n)
Asked in : Facebook.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Nov, 2022
Like Article
Save Article