784. Letter Case Permutation
Description
Given a string s
, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Example 1:
Input: s = "a1b2" Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: s = "3z4" Output: ["3z4","3Z4"]
Constraints:
1 <= s.length <= 12
s
consists of lowercase English letters, uppercase English letters, and digits.
Solutions
Solution 1: DFS
Since each letter in \(s\) can be converted to uppercase or lowercase, we can use the DFS (Depth-First Search) method to enumerate all possible cases.
Specifically, traverse the string \(s\) from left to right. For each letter encountered, you can choose to convert it to uppercase or lowercase, and then continue to traverse the subsequent letters. When you reach the end of the string, you get a conversion scheme and add it to the answer.
The method of converting case can be implemented using bitwise operations. For a letter, the difference between the ASCII codes of its lowercase and uppercase forms is \(32\), so we can achieve case conversion by XORing the ASCII code of the letter with \(32\).
The time complexity is \(O(n \times 2^n)\), where \(n\) is the length of the string \(s\). For each letter, we can choose to convert it to uppercase or lowercase, so there are \(2^n\) conversion schemes in total. For each conversion scheme, we need \(O(n)\) time to generate a new string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Solution 2: Binary Enumeration
For a letter, we can convert it to uppercase or lowercase. Therefore, for each letter, we can use a binary bit to represent its conversion scheme, where \(1\) represents lowercase and \(0\) represents uppercase.
First, we count the number of letters in the string \(s\), denoted as \(n\). Then, there are \(2^n\) conversion schemes in total. We can use each bit of a binary number to represent the conversion scheme of each letter, enumerating from \(0\) to \(2^n-1\).
Specifically, we can use a variable \(i\) to represent the current binary number being enumerated, where the \(j\)-th bit of \(i\) represents the conversion scheme of the \(j\)-th letter. That is, the \(j\)-th bit of \(i\) being \(1\) means the \(j\)-th letter is converted to lowercase, and \(0\) means the \(j\)-th letter is converted to uppercase.
The time complexity is \(O(n \times 2^n)\), where \(n\) is the length of the string \(s\). For each letter, we can choose to convert it to uppercase or lowercase, so there are \(2^n\) conversion schemes in total. For each conversion scheme, we need \(O(n)\) time to generate a new string.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|