Here's another solution. We can also use a Bit-Masking approach to solve this problem.
Solution.java
importjava.util.ArrayList;importjava.util.List;/**
* * Letter Case Permutation
*/publicclassSolution{/**
* Helper function to check if
* a character is a digit or not.
*/privatebooleanisDigit(charch){returnCharacter.isDigit(ch);}/**
* Helper function to toggle
* the case of a character.
*/privatechartoggleCharacterCase(charch){returnCharacter.isLowerCase(ch)?Character.toUpperCase(ch):Character.toLowerCase(ch);}privatevoidgetPermutationsWithLetterCaseChange(Stringinput,intindex,Stringoutput,List<String>perms){/**
* Base Case.
*
* If index is equal to the
* input length, then we've
* found a potential output.
*/if(index==input.length()){perms.add(output.toString());return;}charcurrentChar=input.charAt(index);// If the current character// is a digit, then we don't// need to perform any case// change. We just add it to// the output string.if(isDigit(currentChar)){getPermutationsWithLetterCaseChange(input,index+1,output+currentChar,perms);}else{// First, we first toggle the// case of the current character// and consider all possible// scenarios.getPermutationsWithLetterCaseChange(input,index+1,output+toggleCharacterCase(currentChar),perms);// Second, we don't first toggle the// case of the current character// and consider all possible// scenarios.getPermutationsWithLetterCaseChange(input,index+1,output+currentChar,perms);}}publicList<String>getPermutationsWithLetterCaseChange(Stringstring){List<String>perms=newArrayList<>();getPermutationsWithLetterCaseChange(string,0,"",perms);returnperms;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's another solution. We can also use a Bit-Masking approach to solve this problem.
Solution.java