Distinct Permutations of a String Code || Program 19 || Competitive Coding || Learning Monkey ||

  Рет қаралды 274

Learning Monkey

Learning Monkey

Күн бұрын

Distinct Permutations of a String Code
In this class, We discuss Distinct Permutations of a String Code.
The reader can take the complete competitive coding course. Click Here.
Question:
Given a string S.
Display distinct permutations of the string in lexicographically sorted order.
Example:
Input: ABC
Output: ABC, ACB, BAC, BCA, CAB, CBA
A complete explanation of the logic is provided in the video.
The explanation helps a lot in understanding the concept of recursion.
Code:
class Solution:
def shouldSwap(self,string, start, curr):
for i in range(start, curr):
if string[i] == string[curr]:
return 0
return 1
def findPermutations(self,string, index, n,z):
if index gt= n:
z.append(''.join(string))
return
for i in range(index, n):
check = self.shouldSwap(string, index, i)
if check:
string[index], string[i] = string[i], string[index]
self.findPermutations(string, index + 1, n,z)
string[index], string[i] = string[i], string[index]
def find_permutation(self, S):
string=list(S)
x = len(string)
z=[]
self.findPermutations(string,0,x,z)
#z1=sorted(z)
return z
s=input()
ob=Solution()
k=ob.find_permutation(s)
print(k)
Link for playlists:
/ @learningmonkey
Link for our website: learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ learningmonkey01@gmail.com

Пікірлер: 1
@Lav____sahu21
@Lav____sahu21 Жыл бұрын
sir start java classes
String permutation algorithm | All permutations of a string
14:59
Towers of Hanoi: A Complete Recursive Visualization
21:13
Reducible
Рет қаралды 452 М.
L12. Print all Permutations of a String/Array | Recursion | Approach - 1
19:07
What is a Monad? - Computerphile
21:50
Computerphile
Рет қаралды 598 М.
10 FORBIDDEN Sorting Algorithms
9:41
Ardens
Рет қаралды 832 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 265 М.
Find if one string is a rotation of another string
9:19
Techdose
Рет қаралды 49 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 855 М.