The question can be found here https://leetcode.com/problems/letter-case-permutation
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.
My question pertains mostly to the pattern that I am following. I find that with alot of these leetcode question that I want to use recursion for I find myself making two versions of a function. One which acts as the "interface" is this case
letterCasePermutation
and one which acts as the worker (letterCasePermutationAux
) where the only difference is the arguments (I would usually have something like a counter and a reference to a vector to hold my answers).
I choose this question as an example but really any recursive question could act as a stand in here.
class Solution(object):
ans = []
def letterCasePermutationAux(self, S, i=0):
if i >= len(S):
self.ans.append(S)
return
if S[i].isalpha():
temp = list(S)
temp[i] = S[i].upper() if S[i].islower() else S[i].lower()
self.letterCasePermutationAux("".join(temp), i+1)
self.letterCasePermutationAux(S,i+1)
def letterCasePermutation(self, S):
self.ans=[]
self.letterCasePermutationAux(S)
return self.ans
My question is, is there a nicer way I can go about accomplishing this task? One which can perform recursion only using one function?
self
parameter? Is this part of a class? \$\endgroup\$