Letter Case Permutation
Return every string you can make by switching each letter of a string between lowercase and uppercase.
Given a string s
Example 1
Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
Example 2
Output: ["3z4", "3Z4"]
The Idea
Picture typing a1b23z4
With two letters the four strings are easy to list by hand. With five letters there are 32, and with ten there are 1,024. How do we produce all of them, for any string, without missing one or listing one twice?
Decide one character at a time, from left to right, and write the decisions down as a tree. Look at the picture below.
The top box, in the row labelled start, holds the string before any decision. In the row labelled "position 0: a", the tree splits in two: one branch keeps lowercase a, the other makes it uppercase A. The orange character in each box is the one just decided.
The rows labelled "position 1: 1" and "position 3: 2" have one arrow per box, marked "keep 1" and "keep 2", because a digit has no other case. The row labelled "position 2: b" splits every box in two again. Each path from the top to a teal box at the bottom is one finished string, and the four paths give exactly the four strings of Example 1.
So every letter doubles the number of paths and every digit leaves it unchanged. A string with L letters has 2L results, one per path through its tree.
How do we visit every path? Go down the left-most path to a1b2a1B2
Solution Approach
Turning the tree into code means settling four things: how to walk the tree, where to keep the string being built, what to do at the end of a path, and how to change a letter's case.
Walking the tree with a method that calls itself
How does the code go down a path and come back? A method buildindexindex + 1
When one of these calls returns, the code is back at the split it came from, ready to try the other branch. So stepping back up the tree needs no extra work: it is simply a call returning. A method like build
One char array, changed and put back
Where does the string being built live? A simple way is to pass a new Stringbuildchar[]s
At a letter, buildindex
The end of a path
How does buildindexnew String(chars)
Changing a letter's case
How do we tell a letter from a digit and switch its case? Character.isLetter(c)Character.toLowerCase(c)Character.toUpperCase(c)"Ab""ab"
As an option, a bit trick does the switch for the English letters a to z. Their codes differ from the uppercase ones by exactly 32: 'a''A'(char) (c ^ 32)'b''B''B''b'
Java Solution
The method letterCasePermutationbuild
- First,
creates an empty listletterCasePermutation , turnsresult into as array withchar[] , and callss.toCharArray() with that array andbuild 0.index - In
, ifbuild equalsindex , every position has been decided: addchars.length tonew String(chars) and return.result - Otherwise, read the character at
intoindex . If it is a letter, write its lowercase form and callc forbuild . Then write its uppercase form and callindex + 1 forbuild again. Finally, putindex + 1 back.c - If
is a digit, leave it as it is and callc forbuild once.index + 1 - When the first call to
returns, every path has been walked, andbuild returnsletterCasePermutation .result
The main"Ab""2024""abc"
import java.util.ArrayList;
import java.util.List;
public class LetterCasePermutation {
public static List<String> letterCasePermutation(String s) {
List<String> result = new ArrayList<>();
build(s.toCharArray(), 0, result);
return result;
}
private static void build(char[] chars, int index, List<String> result) {
if (index == chars.length) {
result.add(new String(chars)); // every position decided: one answer
return;
}
char c = chars[index];
if (Character.isLetter(c)) {
chars[index] = Character.toLowerCase(c); // first choice: lowercase
build(chars, index + 1, result);
chars[index] = Character.toUpperCase(c); // second choice: uppercase
build(chars, index + 1, result);
chars[index] = c; // put the original back
} else {
build(chars, index + 1, result); // a digit stays as it is
}
}
public static void main(String[] args) {
System.out.println(letterCasePermutation("a1b2"));
System.out.println(letterCasePermutation("3z4"));
System.out.println(letterCasePermutation("Ab"));
System.out.println(letterCasePermutation("2024"));
System.out.println(letterCasePermutation("abc").size());
}
}
Running it prints:
[a1b2, a1B2, A1b2, A1B2]
[3z4, 3Z4]
[ab, aB, Ab, AB]
[2024]
8
Look at the first line: a1b2, a1B2, A1b2, A1B2build
The third line starts with ab"Ab"Ab"2024""abc"
Complexity
The work is one call of build
- Time: O(2L ยท n) for a string of n characters with L letters, because each of the 2L paths makes at most n + 1 calls and copies n characters into a new
.String - Space: O(n) besides the output, because the calls go at most n + 1 deep and they all share one
array of n characters.char[]