0

I'm looking at the solution of problem asking for all the possible permutations in a string. The output is the amount of permutation combinations and the list of possible permutations. In the Search() function, after the recursive command "search(curr+chr(ord("a") + i))" is called, what does the "char_count[i] += 1" do?

s = input()
perms = []
char_count = [0] * 26


def search(curr: str = ""):
    # we've finished creating a permutation
    if len(curr) == len(s):
        perms.append(curr)
        return
    for i in range(26):
        # For all available characters
        if char_count[i] > 0:
            # Add it to the current string and continue the search
            char_count[i] -= 1
            search(curr + chr(ord("a") + i))
            char_count[i] += 1


for c in s:
    char_count[ord(c) - ord("a")] += 1

search()

print(len(perms))
for perm in perms:
    print(perm)

I don't really understand what that does in the recursive portion of the code.

Phantom
  • 9
  • 2
  • 1
    `char_count[i] += 1` increments a value in an array. If you want to understand this in the context of the entire algorithm, I recommend adding `print()` statements in your code at strategic places to mark where the code is exeucting and the values of important variables at that exact time. – Code-Apprentice Jun 23 '23 at 23:04
  • What does "Codde" mean? (in title) – Bill Jun 23 '23 at 23:08
  • @Bill Typo for "Code" most likely. – Barmar Jun 23 '23 at 23:46
  • Do you understand the `+=` operator in general? If not, you need to go back to your Python tutorial and learn basic operations. And if you do know about it, why do you think it has any different meaning in this context? – Barmar Jun 23 '23 at 23:47

0 Answers0