String Permutations by changing case (medium)
Problem Statement
Given a string, find all of its permutations preserving the character sequence but changing case.
Example 1:
Input: "ad52"
Output: "ad52", "Ad52", "aD52", "AD52"
Example 2:
Input: "ab7c"
Output: "ab7c", "Ab7c", "aB7c", "AB7c", "ab7C", "Ab7C", "aB7C", "AB7C"
Try it yourself
Try solving this question here:
import java.util.*;class LetterCaseStringPermutation {public static List<String> findLetterCaseStringPermutations(String str) {List<String> permutations = new ArrayList<>();// TODO: Write your code herereturn permutations;}public static void main(String[] args) {List<String> result = LetterCaseStringPermutation.findLetterCaseStringPermutations("ad52");System.out.println(" String permutations are: " + result);result = LetterCaseStringPermutation.findLetterCaseStringPermutations("ab7c");System.out.println(" String permutations are: " + result);}}
Solution
This problem follows the Subsets pattern and can be mapped to Permutations.
Let’s take Example-2 mentioned above to generate all the permutations. Following a BFS approach, we will consider one character at a time. Since we need to preserve the character sequence, we can start with the actual string and process each character (i.e., make it upper-case or lower-case) one by one:
- Starting with the actual string:
"ab7c"
- Processing the first character (‘a’), we will get two permutations:
"ab7c", "Ab7c"