Post

Permutations of given String

Ref link1

Ref link2

Sudo Code

1
2
1) Solve using recursion
2) Take index and swap it with each element in the array

alt text

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def permutat(arr, l, r):

    if l == r:
        print("".join(arr))
        return
    
    for i in range(l,r,1):
        arr[l], arr[i] = arr[i], arr[l]
        permutat(arr, l+1, r)
        arr[l], arr[i] = arr[i], arr[l] 


string = "ABC"
n = len(string) 
a = list(string) 
  
# Function call 
permutat(a, 0, n) 

Leetcode

This post is licensed under CC BY 4.0 by the author.