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
Gym belt !! 😂😂  @kauermotta
00:10
Tibo InShape
Рет қаралды 18 МЛН
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 118 МЛН
Amazing weight loss transformation !! 😱😱
00:24
Tibo InShape
Рет қаралды 66 МЛН
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 50 МЛН
L13. Print all Permutations of a String/Array | Recursion | Approach - 2
18:14
Hollow Diamond Pattern In Java || STEP BY STEP
18:11
Java & Web Coding By Ritu
Рет қаралды 198
C Program #30: Display all possible permutations of given string
12:08
Learn TechToTech
Рет қаралды 24 М.
BREAKING Chess Drama As Niemann Rips Into Magnus Carlsen
8:25
Epic Chess
Рет қаралды 50 М.
Find if one string is a rotation of another string
9:19
Techdose
Рет қаралды 49 М.
L12. Print all Permutations of a String/Array | Recursion | Approach - 1
19:07
Permutation | JavaScript Tutorial | LetCode
7:14
LetCode with Koushik
Рет қаралды 10 М.
Gym belt !! 😂😂  @kauermotta
00:10
Tibo InShape
Рет қаралды 18 МЛН