Permutations Iteratively

preview_player
Показать описание
Write a program to generate all permutations of a string without using recursion!
Make sure you're pausing the video and having a go along the way.
Рекомендации по теме
Комментарии
Автор

How did you reach this level in programming?

BlueSkyGoldSun
Автор

I'm a noob and I spend over 3 hours trying to do this on my own. So frustrating.

mw
Автор

thx for this video.
The intuition here that I was struggling with was that the letter just needs to be added in between the existing substring.
I feel the "stack" here unnecessary you are simply iterating over all the letters.
here is "my" solution:
def iter_perm(string: str) -> list[str]:
if len(string) < 2:
return string

retval = set()
retval.add(string[0])
for l in string[1:]:
new_retval = set()

for val in retval:
for idx in range(len(val)+1):
new_retval.add(val[:idx] + l + val[idx:])

retval = new_retval

return retval

darkenblade