103k views
2 votes
You will be given a string, containing both uppercase and lowercase alphabets(numbers are not allowed).

You have to print all permutations of string with the added constraint that you can’t change the uppercase alphabets positions.


Example: (puNeeTgUlia)

User Akcoban
by
2.5k points

1 Answer

5 votes

Answer:

The Java code is given below with appropriate comments

Step-by-step explanation:

import java.util.*;

class Main {

static Set<String> set = new HashSet();

static void printPerms(char ch[], int ind){

//If end of string is reached, add it to set

if(ind==ch.length){

set.add(new String(ch));

return;

}

for(int i=ind;i<ch.length;i++){

//Only swap if lower case

if((ch[i]>='a' && ch[i]<='z')&&((ch[ind]>='a' && ch[ind]<='z'))){

char t = ch[i];

ch[i] = ch[ind];

ch[ind] = t;

}

printPerms(ch,ind+1);

if((ch[i]>='a' && ch[i]<='z')&&((ch[ind]>='a' && ch[ind]<='z'))){

char t = ch[i];

ch[i] = ch[ind];

ch[ind] = t;

}

}

}

public static void main(String[] args) {

printPerms("aBbCc".toCharArray(),0);

System.out.println(set);

}

}

User Dan Forbes
by
3.2k points