Solution: Letter Case Permutation
Let’s solve the Letter Case Permutation problem using the Subsets pattern.
We'll cover the following...
Statement
Given a string, s
, consisting of letters and digits, generate all possible variations by modifying each letter independently to be either lowercase or uppercase while keeping the digits unchanged. Each letter in the s
string can appear in both cases across different variations, resulting in multiple possible combinations. Return a list containing all such variations in any order.
Constraints:
s.length
s
consists of lowercase English letters, uppercase English letters, and digits.
Solution
The solution follows the subsets pattern to generate all possible letter case permutations for s
. The idea is to start with a result list containing an empty string. To build variations step by step, it iterates through s
, processes each character, and updates the result list to store the variations of the current character. For each character in s
: If it is a letter, create two permutations for each existing string in the result
—one by appending the lowercase version and another by appending the uppercase version. Otherwise, if it is a digit, append it to each string in the result
without any modifications. After processing all characters, the result
contains all possible letter case combinations.
Now, let’s look at the solution steps in detail:
We initialize a list,
result
, containing an empty string. This serves as the base case for building permutations.We iterate through each character,
ch
, of thes
, and for each character:If
ch
is a letter:We Iterate through each string,
str
, inresult
, and to create permutations:We append the lowercase version of
ch
tostr
to create the first one.We append the uppercase version of
ch
tostr
to create the second one.
If
ch
is a digit:We Iterate through each string,
str
, inresult
, and each iteration:We append the same digit to
str
to create the permutation.
After iterating through all the characters in
s
,result
contains all possible letter-case permutations.
Let’s look at the following illustration to get a better understanding of the solution:
Let’s look at the code for the solution we just discussed.
package mainimport ("fmt""strings""unicode")func letterCasePermutation(s string) []string {result := []string{""}for _, ch := range s {size := len(result)for i := 0; i < size; i++ {str := result[i]if unicode.IsLetter(ch) {result[i] = str + string(unicode.ToLower(ch))result = append(result, str+string(unicode.ToUpper(ch)))} else {result[i] = str + string(ch)}}}return result}// Driver functionfunc main() {stringsList := []string{"a1b2", "3z4", "ABC", "123", "xYz"}for i, s := range stringsList {fmt.Printf("%d.\ts: \"%s\"\n", i+1, s)output := letterCasePermutation(s)fmt.Print("\n\tOutput: [")for j, val := range output {fmt.Printf("\"%s\"", val)if j < len(output)-1 {fmt.Print(", ")}}fmt.Println("]\n" + strings.Repeat("-", 100))}}
Time complexity
The time complexity of the solution is