8 Permutation of Strings | Backtracking Solution

  Рет қаралды 15,877

Aditya Verma

Aditya Verma

Күн бұрын

Given a string S. The task is to print all unique permutations of the given string in lexicographically sorted order.
Example 1:
Input: ABC
Output:
ABC ACB BAC BCA CAB CBA
Explanation:
Given string ABC has permutations in 6
forms as ABC, ACB, BAC, BCA, CAB and CBA .
Example 2:
Input: ABSG
Output:
ABGS ABSG AGBS AGSB ASBG ASGB BAGS
BASG BGAS BGSA BSAG BSGA GABS GASB
GBAS GBSA GSAB GSBA SABG SAGB SBAG
SBGA SGAB SGBA
Explanation:
Given string ABSG has 24 permutations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function find_permutation() which takes the string S as input parameter and returns a vector of string in lexicographical order.
Expected Time Complexity: O(n! * n)
Expected Space Complexity: O(n! * n)
Problem Statement: www.geeksforge...
------------------------------------------------------------------------------------------
Here are some of the gears that I use almost everyday:
🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
💻 : My gaming laptop: amzn.to/3yjcn23
📱 : My Ipad: amzn.to/39yEMGS
✏️ : My Apple Pencil: amzn.to/3kMnKYf
🎧 : My Headphones: amzn.to/3kMOzM7
💺 : My Chair: amzn.to/385weqR
🛋 : My Table: amzn.to/3kMohtd
⏰ : My Clock: amzn.to/3slFUV3
🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

Пікірлер: 53
@wiselife5821
@wiselife5821 5 ай бұрын
mai ek week se struggle kar raha tha dhang ki recursion video ke liye bcoz mai bilkul hi beginner hu. trust me maine boht time lagaya hai recursion samajhne mei . duniya bhar ki videos dekh li par ab jake recursion samajh aya hai(recursion ki playlist bhi dekhi hai fir backtracking par aya hu). to commment likhna to banta hai. jab maine ye backtracking ka code khud se likh diya tab mai pagal ho gaya or agaya comment likhne . shukriya aditya bhai. tum humara bhala kr rhe to tumhara bhala hone se koi nahi rok sakta.
@dsaa2z
@dsaa2z 9 ай бұрын
Hello Sir, I had also started a series for dsa using python on youtube. I am trying to complete strivers atoz sheet. I am deeply impressed by your teaching style. Thanks for creating such awesome content.
@ritwikvaidya5754
@ritwikvaidya5754 5 ай бұрын
One small detail he missed which I found while tying Permutations question on Leetcode. In the backtracking step, you need to do mp.erase(s[i]) in addition to swap(s[start],s[i]). If you don't do this, the recursive tree will get pruned after the very first character itself.
@JangBahadur3028
@JangBahadur3028 9 ай бұрын
Was waiting for this videos after the last 😊
@saumyagaur7633
@saumyagaur7633 8 ай бұрын
Great video. BT never felt this easy
@Raj10185
@Raj10185 9 ай бұрын
Watched the video and successfully solved permutation 1 and 2 of the leetcode
@GoluKumar-sb2si
@GoluKumar-sb2si 2 ай бұрын
me too
@prrockzed
@prrockzed 9 ай бұрын
Was waiting for the video.... thanks a lot
@rishabhahuja7413
@rishabhahuja7413 9 ай бұрын
Graphs and Trees please🙏
@anushakandagal1727
@anushakandagal1727 4 ай бұрын
An extra check condition can be applied to avoid some redundant function calls, if 2 chars are same there is no point in swapping them/undo the swapping class Solution { public: vectorfind_permutation(string S) { // Code here there set ans; permutations(S,0,ans); vector vc(ans.begin(), ans.end()); return vc; } void permutations(string &s, int start, set &ans){ if(start == s.size()-1){ ans.insert(s); return; } unordered_map mp; for(int i = start; i < s.size(); i++){ if(mp.find(s[i]) == mp.end()){ mp[s[i]]++; if(s[i] != s[start]){ swap(s[i],s[start]); permutations(s,start+1,ans); // backtracking step swap(s[i],s[start]); } else permutations(s,start+1,ans); } } } }; This solution works on gfg practice question www.geeksforgeeks.org/problems/permutations-of-a-given-string2041/1
@049bite_gauravkumarsoni3
@049bite_gauravkumarsoni3 9 ай бұрын
please make a video of Trees and Graph also.. ❣
@anutoshghosh7893
@anutoshghosh7893 6 ай бұрын
Very well explained. So basically, here the flow is similar to DFS here right and not BFS, just wanted to clarify that?
@gamingbeast3555
@gamingbeast3555 8 ай бұрын
aur bhaiya aapsase pdnae kae baad aur practice kae baad questions ho rhae hain mjhsae.. thanks a lot bhaiya when i will get offer i will meet you bhaiyaa
@sumitbasu5146
@sumitbasu5146 4 ай бұрын
Thank you Aditya Verma Salute 🥷
@gamingbeast3555
@gamingbeast3555 8 ай бұрын
Thanks a lot bhaiya for such great videos... bs bhaiya tree aur graph ki series aur laa do finall request bhaiya..
@randomsongs8835
@randomsongs8835 8 ай бұрын
Hello Sir, Please complete this series. It is really helpful
@bommakantibeeraiah1486
@bommakantibeeraiah1486 5 ай бұрын
private static List permutations(String s) { List ans=new ArrayList(); int idx=0; solve(idx,s.toCharArray(),ans); return ans; } private static void solve(int idx,char[] s, List ans) { if(idx==s.length-1) { String str=""; for(char c:s) { str+=c; } ans.add(str); return; } Set st=new HashSet(); for(int i=idx;i
@jaspalsharma7426
@jaspalsharma7426 Ай бұрын
Sir, please also make video on graph
@AjayThakur-gu9hg
@AjayThakur-gu9hg 9 ай бұрын
Kindly complete the dp series... you missed some patterns mentioned in 1 st video
@divyranjan254
@divyranjan254 5 ай бұрын
the unordered set shown in recursive code at 2:40 needs to be declared globally or passed by reference just like the vector to store answers because in the current code, each recursive call would form a new unordered_set making its purpose to avoid repetition useless.
@srajanratti8093
@srajanratti8093 2 ай бұрын
No that would stop exploration further. You can try coding. I did the same and was not getting all the results. We only want to limit on a particular level.
@kullumanali-bu8uf
@kullumanali-bu8uf 7 ай бұрын
you're one of best
@abdulmohsin1
@abdulmohsin1 7 ай бұрын
Aditya bro, Voice thodi low hai video ki, please increase it a bit if possible, thank you so much
@sadafkausar8770
@sadafkausar8770 9 ай бұрын
Hello sir, recently I completed your recursion playlist so next what should I start your backtracking playlist or your DP playlist.
@surajsingh-sm7qx
@surajsingh-sm7qx 7 ай бұрын
DP then BT
@Doraemon67812
@Doraemon67812 9 ай бұрын
Bhaiya campus se placement hua tha pr offer letter revoke ho gya bhot jagah job ke liye apply kar chuka hu pr response nhi aa rha please koi job bta do
@saurabhjalutharia
@saurabhjalutharia 9 ай бұрын
volume thoda high m record kiya kro, full volumne m bhi low lgti h
@Prateek_Mantry
@Prateek_Mantry 4 ай бұрын
thank you.
@TanuKansal-nn4el
@TanuKansal-nn4el 8 ай бұрын
It's showing 2 unavailable videos. Is there any video published after permutation question ?
@abhi224505
@abhi224505 6 ай бұрын
Not sure string will work in case of javascript. Maybe we can just use array in javascript, as it is pass by value. function find_permutation(arr) { const res = []; const swap = (arr, i, j) => { [arr[i], arr[j]] = [arr[j], arr[i]]; }; function solve(arr, start) { if (start === arr.length) { res.push([...arr]); } let map = {}; for (let i = start; i < arr.length; i++) { if (!map[arr[i]]) { map[arr[i]] = true; swap(arr, start, i); solve(arr, start + 1); swap(arr, start, i); } } } solve(arr, 0); return res; } console.log(find_permutation([1, 2, 3, 3]));
@raunakgiri21
@raunakgiri21 5 ай бұрын
*Converting the string to array makes it work* find_permutation(S){ //code here let res = []; let strArr = S.split(''); const swapCharacters = (index1, index2) => { let t = strArr[index1]; strArr[index1] = strArr[index2]; strArr[index2] = t; } const backtrack = (ind) => { if(ind === strArr.length) { res.push(strArr.join('')); return; } const map = new Map(); for(let i=ind;i
@Raj10185
@Raj10185 9 ай бұрын
Awsome video maza aa gaya
@eshaansharma145
@eshaansharma145 7 ай бұрын
The time complexity (TC) of the provided code is O(N * N!), where N is the length of the input string S. Here's the breakdown: Sorting the input string takes O(N * log(N)) time. The backtracking algorithm generates all permutations, and there are N! possible permutations for a string of length N. For each permutation, the function makes swaps and checks for duplicates, contributing to a linear factor of N for each permutation. Therefore, the overall time complexity is O(N * log(N) + N * N!). The dominating factor is N!, making the time complexity effectively O(N * N!).
@pranabpaul6317
@pranabpaul6317 6 ай бұрын
The average case time complexity of an unordered_set is O(1) and worst case O(n), here we can also use the unordered_set as we don't need to maintain sorted order in the set. So why you are taking O(logn) in account and also sorting?
@gurupreetsingh8347
@gurupreetsingh8347 9 ай бұрын
Nice
@aniketsharma472
@aniketsharma472 8 ай бұрын
SIr, please complete this series.....
@deepanshuverma6237
@deepanshuverma6237 2 ай бұрын
Permutation of a List of Integers in java : private static void solve(int idx, List s, List ans) { if (idx == s.size() - 1) { ans.add(new ArrayList(s)); return; } Set st = new HashSet(); for (int i = idx; i < s.size(); i++) { if (!st.contains(s.get(i))) { st.add(s.get(i)); Collections.swap(s, idx, i); solve(idx + 1, s, ans); Collections.swap(s, idx, i); } } }
@kullumanali-bu8uf
@kullumanali-bu8uf 7 ай бұрын
this code is working even if we pass the string by value ...why??
@gamingbeast3555
@gamingbeast3555 8 ай бұрын
Bhaiya aapsae request hai aapk tree and graph ki series aur pda do please bhaiya..
@kalpagarwal8291
@kalpagarwal8291 4 ай бұрын
waiting for generating unique permutations.
@pratik9449
@pratik9449 8 ай бұрын
15:15 main video starts here
@xiaoshen194
@xiaoshen194 9 ай бұрын
To ab main sum string wala ques kr paunga gfg pe?
@ratneshtiwari2028
@ratneshtiwari2028 8 ай бұрын
Subarrays with Sum ‘k' please help me with this problem'
@_desouvik
@_desouvik 4 ай бұрын
@SaurabhMishraa
@SaurabhMishraa 9 ай бұрын
❤❤❤❤❤❤
@siddarthchidurula5931
@siddarthchidurula5931 8 ай бұрын
Trees padhlo bhai...striver ki pattern samajh nahi a raha hai
@sumitbasu5146
@sumitbasu5146 4 ай бұрын
Tree Tree Tree Please please please
@abhayjoshi362
@abhayjoshi362 9 ай бұрын
ye kaisi baat kr rahi hai aap devi ji🤓
@ratneshtiwari2028
@ratneshtiwari2028 9 ай бұрын
tree
@Doraemon67812
@Doraemon67812 9 ай бұрын
Bhai itni coding ki kuch kan nhi aaya
@xiaoshen194
@xiaoshen194 9 ай бұрын
Kyu 24 batch ka h?
9 Largest number in K swaps
36:47
Aditya Verma
Рет қаралды 15 М.
7 Time Complexity of a Recursive Tree
25:43
Aditya Verma
Рет қаралды 11 М.
6 Permutation of Strings | Simple Recursion
39:00
Aditya Verma
Рет қаралды 24 М.
Leetcode 46. Permutations : Introduction to backtracking
10:06
ComputerBread
Рет қаралды 95 М.
The Most Important Design Pattern in React
35:04
Cosden Solutions
Рет қаралды 34 М.
23  Printing Longest common subsequence
26:45
Aditya Verma
Рет қаралды 234 М.
AIR 1 Took his life !! 😔 | Every Student need to listen this - Alakh sir
11:13
Resources I Followed to Crack Placement in 3 month's 💯
10:44
Ajay Raj
Рет қаралды 1 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 142 М.