Leetcode: Letter case permutation15 Sep 2022 by Friedrich Ewald · 2 min read
The task is to print all case permutations of letters from strings that can include digits and letters. For example,
a1b, a1B, A1b, A1B.
The trick here is to realize that there are
2^n possible permutations where
n is the number of characters, excluding digits.
One possible solution is shown below. The main idea is to generate binary numbers and then use the
0 in their places to either make the character uppercase or keep it lowercase. For example a decimal number
2 is represented by
b10 in binary notation. The number
3 is represented by
b11, and so on. When left padding - meaning to write a prefix of
0, all possible combinations are found.
Runtime: 106 ms, faster than 33.96% of Python3 online submissions for Letter Case Permutation.
Memory Usage: 14.6 MB, less than 81.20% of Python3 online submissions for Letter Case Permutation.