careercup/CtCI-6th-Edition-Python

chapter 8 , 8.9 parens

parthpatel1470 opened this issue · 1 comments

Can we use this code as an alternative solution for this problem

def print_perms(n):
    result = []
    letter_count_map = {'(':n,')':n}
    print(letter_count_map)
    print_perms_inner(letter_count_map, "", n+n, result)
    return result



def print_perms_inner(letter_count_map, prefix, remaining, result):
    # base case Permutation has been completed
    if remaining == 0:
        # if prefix[0]=='(' and prefix[5]==')':
        result.append(prefix)
        return
    # try remaining letter for next char, and generate remaining permutations
    for character in letter_count_map:
        count = letter_count_map[character]
        if count > 0 and letter_count_map['(']<=letter_count_map[')']:
            letter_count_map[character] -= 1
            print_perms_inner(
                letter_count_map, prefix + character, remaining - 1, result
            )
            letter_count_map[character] = count

I have just changed some part in code of 8.8 permuations with dups

Yes seems like that would work.