Grokking the Coding Interview: Patterns for Coding Questions
0% completed
String Permutations by changing case (medium)
Problem Statement
Given a string, find all of its permutations preserving the character sequence but changing case.
Example 1:
Input: "ad52"
Output: "ad52", "Ad52", "aD52", "AD52"
Example 2:
Input: "ab7c"
Output: "ab7c", "Ab7c", "aB7c", "AB7c", "ab7C", "Ab7C", "aB7C", "AB7C"
Constraints:
1 <= str.length <= 12strconsists of lowercase English letters, uppercase English letters, and digits.
Try it yourself
Try solving this question here:
.....
.....
.....
Like the course? Get enrolled and start learning!
Mohammed Dh Abbas
· 2 years ago
class Solution: def findLetterCaseStringPermutations(self, text): def backtrack(word, result, seen, index): result.append(word[:]) for i in range(index, len(word)): # loop through each character in the text char = word[i] if i not in seen and not char.isnumeric(): # if the character is not a number and have no been seen seen.add(i) new_word = word[:i] + char.swapcase() + word[i + 1:] backtrack(new_word, result, seen, i) # recurse back seen.remove(i) result = [] backtrack(text, result, set(), 0) return result
Show 1 reply