Letter Case Permutation
excerpt: Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. tags: permutation recursion backtracking exhaustive-enumeration
Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create. You can return the output in any order.
Example 1:
Input: S = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: S = "3z4"
Output: ["3z4","3Z4"]
Example 3:
Input: S = "12345"
Output: ["12345"]
Example 4:
Input: S = "0"
Output: ["0"]
Constraints
- S will be a string with length between 1 and 12.
- S will consist only of letters or digits.
Problem Solving Process
Analyze the Problem
Define the Interface
Input: String s
Output: Array of strings
Classify the problem
- Is this similar to a problem you have already seen before? Permutation.
- Exhaustive Enumeration (Clue: All possible strings)
- Order does not matter
The string s consists of letter or digits and the length is between 1 and 12. If the input consists only of numbers, just return it. We will have multiple permutation strings only if letters and digits or letters are given as the input string. Only if you see a letter then you process it, otherwise we append it to the result.
Work with Small Examples
n = 1
‘a’
‘a’, ‘A’
n = 2
“ab”
‘a’, ‘A’
‘ab’, ‘Ab’, ‘aB’, ‘AB’
n = 3
‘abc’
‘abc’, ‘Abc’, ‘aBc’, ‘ABc’, ‘abC’, ‘AbC’, ‘aBC’, ‘ABC’
outer loop 0 to length of the input inner loop
How to Implement?
- Solution will be very similar to the permutation problem
- We cannot change the index, we only need to either uppercase or lowercase. Processing consists of two choices, upper/lower.
- What approach can we use to solve this problem?
- Recursion
- Iteration
- Recursive Approach
- Backtracking
- We need to keep track of the index
- Why does the index need to be kept track of?
- So that we know which letter needs to transformed to upper or lower case
- Base Case
- If the current index = length of the given string
- Append the permutation string to the output array
- Unit of Work Process one letter or one digit If it is letter => uppercase/lowercase If it is a digit. => append it to the output
- Do we do this unit of work before the recursive call or after the recursive call?
- Before the recursive call
- Do we want to return a value or is it returning void?
- Build a partial solution that will eventually become a permutation
- Do we pass it in as a parameter to the function?
- How do we keep track of the output?
- parameter of the function
- Define a wrapper/helper function
- How many recursive calls?
- Two recursive calls for letters Two choices: upper/lower
- One recursive call for digits
Time and Space Complexity
Time: O(2^nn) Space: O(2^nn)
Implementation
|
|
The problem requires us to find all the possible strings by changing the case of the letters in the given string s
. Here’s a step-by-step guide to solve this problem:
- Initialize the Result: Create a list to hold the results.
- Iterate through the String: Go through each character in the given string
s
. - Handle Letters and Digits Differently: If the current character is a letter, both lower and upper cases are possible. If it’s a digit, there’s only one possibility.
- Use Recursion or Iteration: You can either use recursion to build the strings character by character or use an iterative approach to handle the permutations.
- Return the Result: Finally, return the list containing all the permutations.
Here’s a solution using an iterative approach:
|
|
Explanation:
- For each character in
s
, if it’s a letter, we add both the lower and upper cases to the current results. - If it’s a digit, we simply add it to the current results.
- We use list comprehension to create the new results at each step.
Analyze the Problem
Define the Interface Input: String s Output: Array of strings
Classify the problem Is this similar to a problem you have already seen before? Permutation
Exhaustive Enumeration (Clue: All possible strings)
Order does not matter
s consists of letter or digits and the length is between 1 and 12
If the input consists only of numbers, just return it. We will have multiple permutation strings only if letters and digits or letters are given as the input string.
Only if you see a letter then you process it, otherwise we append it to the result.
How to Implement?
Solution will be very similar to the permutation problem
We cannot change the index, we only need to either uppercase or lowercase Processing: Two choices, uppper/lower
What approach can we use to solve this problem? Recursion Iteration
Recursive Approach
- Backtracking
- We need to keep track of the index
- Why index needs to be kept track of?
- So that we know which letter needs to transformed to upper or lower case
- Base Case
- If the current index = length of the given string
- Append the permutation string to the output array
- Unit of Work Process one letter or one digit If it is letter => uppercase/lowercase If it is digit. => append it to the output
- Do we do this unit of work before the recursive call or after the recursive call?
- Before the recursive call
- Do we want to return a value or is it returning void?
- Build a partial solution that will eventually become a permutation
- Do we pass it in as a parameter to the function?
- How do we keep track of the output?
- parameter of the function
- Define a wrapper/helper function
- How many recursive calls?
- Two recursive calls for letters Two choices: upper/lower
- One recursive call for digits
|
|