Print unique subsets And Variations

  Рет қаралды 111,700

Aditya Verma

Aditya Verma

Күн бұрын

Пікірлер: 248
@piyushji2312
@piyushji2312 4 жыл бұрын
Bhai fit-jee ke baad ab jaakar aisa teacher mila hai. :' )
@RavinderKumar-bn4ch
@RavinderKumar-bn4ch 4 жыл бұрын
VERY TRUE
@rahulgunkar6308
@rahulgunkar6308 3 жыл бұрын
Agreed
@carry4398
@carry4398 4 жыл бұрын
Itna detailed analysis kahi aur nhi milega.. guarantee
@VKBMath
@VKBMath 3 жыл бұрын
Pep Coding par sumeet sir
@shivamsinha5554
@shivamsinha5554 3 жыл бұрын
@@VKBMath ya bro he is also best 🙂
@pr1493
@pr1493 3 жыл бұрын
ghanta
@tarunbawari2858
@tarunbawari2858 4 жыл бұрын
Bhai apne recursion ko bahut easy kar diya he me bahut kam use karta tha recursion but now i am comfortable with recursion 😅
@ashaykoradia2475
@ashaykoradia2475 2 жыл бұрын
17:10 "Its her choice" - I laughed so hard🤣. Thank you for making these amazing videos. You make things a cakewalk. I am preparing for interviews for product based companies now and one day when I get there, I am definitely returning all the favour of learning from you!🙌
@sanskarsinghiit
@sanskarsinghiit Жыл бұрын
++,
@simranbansal5644
@simranbansal5644 2 жыл бұрын
I have never finished any youtubers playlist till now, but completed this one, the things look so easy. Thank You.
@eshitakalsi7682
@eshitakalsi7682 4 жыл бұрын
How can someone dislike a video as good as this? :(
@mishorpatra1670
@mishorpatra1670 3 жыл бұрын
Dislikes are from Neha Dhupia fans
@akshayverma4540
@akshayverma4540 4 жыл бұрын
Hi Aditya, It would be very helpful if you could also include the time and space complexity in your video as this will definitely increase the quality of your content and will also be helpful for everyone.
@mercenary-coder
@mercenary-coder 10 ай бұрын
mostly recursive code complexity comes as exponential .
@shreyxnsh.14
@shreyxnsh.14 6 ай бұрын
kahan placed ho bhai aajkal?
@radium990
@radium990 Ай бұрын
To solve the problem without using the character logic inside the recursion (i.e., without tracking each character individually), and by storing the subsets directly into a map while checking for duplicates at the base case, we can approach it in the following way: Approach: 1. Recursive Subset Generation: The recursive function generates all subsets of the string by exploring the options of including or excluding the current character. The base condition checks if the input string is empty, and if so, it adds the current subset to a Map. 2. Handling Duplicates: The key idea is to use a Map to store subsets. If a subset has already been generated (present in the map), we simply increment its counter. This avoids storing duplicates. 3. Lexicographical Order: Instead of sorting the string at the start, we will use the map to ensure subsets are stored only once. However, for output to be in lexicographical order, we can simply iterate over the map's keys in sorted order when printing the subsets. Java Code: import java.util.*; public class UniqueSubsetsWithMap { public static void main(String[] args) { String input = "aab"; // Example input string Map subsetMap = new HashMap(); // Map to store subsets and their counts // Start the recursion to generate subsets generateSubsets(input, "", subsetMap); // Sort the map keys to print subsets in lexicographical order List sortedSubsets = new ArrayList(subsetMap.keySet()); Collections.sort(sortedSubsets); // Print the subsets in lexicographical order for (String subset : sortedSubsets) { System.out.println(subset); } } // Recursive function to generate subsets private static void generateSubsets(String input, String currentSubset, Map subsetMap) { // Base case: if the input string is empty, add the current subset to the map if (input.isEmpty()) { subsetMap.put(currentSubset, subsetMap.getOrDefault(currentSubset, 0) + 1); return; } // Recursive case: Exclude the first character and recurse generateSubsets(input.substring(1), currentSubset, subsetMap); // Recursive case: Include the first character and recurse generateSubsets(input.substring(1), currentSubset + input.charAt(0), subsetMap); } } Explanation: 1. Recursion: The generateSubsets function is the recursive function that explores all subsets of the input string. It operates by either including or excluding the first character of the string in the current subset. 2. Base Case: The base case occurs when the input string is empty. At this point, the currentSubset (which holds the current combination of characters) is added to the map with its count. The map ensures that duplicate subsets are counted, but only one instance of each unique subset is stored. 3. Map for Uniqueness: We use a Map to store subsets and their counts. The key is the subset itself, and the value is the count of how many times this subset has been generated. This prevents duplicate subsets from being stored, as we increment the count of the existing subset in the map if it is generated again. 4. Printing in Lexicographical Order: After all subsets have been generated, we retrieve all the unique subsets from the map, sort them lexicographically using Collections.sort(), and then print them. Example Walkthrough for input "aab": Recursion unfolds like this: generateSubsets("aab", "", subsetMap) -> generateSubsets("ab", "", subsetMap) -> generateSubsets("b", "", subsetMap) -> generateSubsets("", "", subsetMap) // Base case: add "" to map -> generateSubsets("", "b", subsetMap) // Base case: add "b" to map -> generateSubsets("b", "a", subsetMap) -> generateSubsets("", "a", subsetMap) // Base case: add "a" to map -> generateSubsets("", "ab", subsetMap) // Base case: add "ab" to map -> generateSubsets("ab", "a", subsetMap) -> generateSubsets("b", "a", subsetMap) -> generateSubsets("", "a", subsetMap) // Base case: add "a" to map -> generateSubsets("", "ab", subsetMap) // Base case: add "ab" to map -> generateSubsets("b", "aa", subsetMap) -> generateSubsets("", "aa", subsetMap) // Base case: add "aa" to map -> generateSubsets("", "aab", subsetMap) // Base case: add "aab" to map Output for input "aab": "" "a" "aa" "aab" "ab" "b" Explanation of the Map: The map after recursion would contain the following entries: {"": 1, "a": 2, "aa": 1, "aab": 1, "ab": 1, "b": 1} After sorting the keys of the map, the subsets are printed in lexicographical order. Time Complexity: Recursion: The recursion explores all subsets, which results in 2^n possible subsets, where n is the length of the input string. Map Operations: Each subset insertion into the map takes O(1) time for each subset. Sorting: After generating all subsets, we sort the map's keys, which takes O(k log k) time, where k is the number of unique subsets (k
@DeepakKumar-e2s5q
@DeepakKumar-e2s5q Ай бұрын
thanks cutiee
@Vishal-ds6ly
@Vishal-ds6ly Жыл бұрын
you are great sir I love the way you teach earlier I used to memorize questions but I now I understand it.
@AmitSingh-cs2hb
@AmitSingh-cs2hb 4 жыл бұрын
Instead of creating map and vector,then comparing that string is present in map or not, we can simply use Set which automatically removes duplicates and also sort the strings.
@anaszubair582
@anaszubair582 4 жыл бұрын
But time complexity will increase as set takes o(logn) for insertion which map takes o(1) for insertion
@ashishchhikara7829
@ashishchhikara7829 4 жыл бұрын
@@anaszubair582 but for checking vector is already present or not in map takes O(logn) and it requires extra space, hence set is better
@anaszubair582
@anaszubair582 4 жыл бұрын
@@ashishchhikara7829 we will use unordered map which take o(1) for searching and yes It will take extra space but it is faster then set
@ashishchhikara7829
@ashishchhikara7829 4 жыл бұрын
@@anaszubair582 sorry bro for misunderstanding.....i used to code in python, and it have dict data structure, so i thought in that way.......i know nothing about C++ or java........from python point of view ....i think set will be better
@anaszubair582
@anaszubair582 4 жыл бұрын
@@ashishchhikara7829 even in python dict has complexity o(1) for insertion and searching
@srajikagupta3627
@srajikagupta3627 4 жыл бұрын
I can see people demanding for other topics in the comment section . But i am here to appreciate the efforts you are making selflessly .
@Colourshred
@Colourshred 3 жыл бұрын
IT'S HER CHOICE :') This content is like heaven for the brain! Godspeed.
@gauravraj2604
@gauravraj2604 3 жыл бұрын
bhai ki hr video is lit... and wo 13:36 p knapsack wali problem yaad meko v aa gyi..
@Khabibullah
@Khabibullah 4 жыл бұрын
Bro give timestamps if possible, BTW suuup explanation 👌👌
@0anant0
@0anant0 4 жыл бұрын
Great explanation! Another option to map and set is to simply sort the input and then skip all duplicates of an element
@TheAdityaVerma
@TheAdityaVerma 4 жыл бұрын
That would give incorrect answer eg: Dry run your approach for "aab" [HINT: Your approach would probably skip "aa", but that must be present]
@0anant0
@0anant0 4 жыл бұрын
@@TheAdityaVerma Oh yes! I was thinking of no duplicates in OUTput, not INput. My bad!
@bhaskarbhakat5689
@bhaskarbhakat5689 4 жыл бұрын
sun meri baat its her choice
@vedantgolash6084
@vedantgolash6084 4 жыл бұрын
hahahahaha
@ru2979
@ru2979 3 жыл бұрын
@@vedantgolash6084 hue hue hue
@surbhitamrakar1638
@surbhitamrakar1638 4 жыл бұрын
The best explanation I ever found ... thanks a lott please make series on graph
@aravindhravi2307
@aravindhravi2307 4 жыл бұрын
meanwhile u can refer wiilliam fiset for graphs
@soniamalik4929
@soniamalik4929 3 жыл бұрын
I am relaxed seriously for future technical interviews coz Aditya explain so well that each concept is mastered.
@dankyt9249
@dankyt9249 4 жыл бұрын
simple code just by using map for unique subset with comments:- #include using namespace std; void subset(string ip,string op,map &m) { if((int)ip.size()==0) { m[op]++; // only these two lines I added additionally checking whether string was present before or not. if(m[op]==1) // If not then print. Rest is full same code as before just declare map and pass by reference. cout
@sahilguleria5505
@sahilguleria5505 4 жыл бұрын
could use set/unordered_set
@geekylearner3596
@geekylearner3596 Жыл бұрын
can u explain first line in if()
@shashikantverma60
@shashikantverma60 6 ай бұрын
@@geekylearner3596 you can directly write ip.length() \
@Raj10185
@Raj10185 Жыл бұрын
Solution for leetcode subsets 2 :- class Solution { public: set ans ; //global bana diya taki bar bar recursion me pass nhi karna pade void generate(vector ip,vector op) { if(ip.size()==0) { sort(op.begin(),op.end()); //odering of output bhi matter krta hai set ke liye nhi ans.insert(op); //abki baar set me insert kar rhe hai return; } // int pick ke jgh ab vector pick/notpick hoga vector notpick = op; vector pick = op ; pick.push_back(ip[0]); // ab push_back ip[0] mtlb 1 row wala pura aa jyega ip.erase(ip.begin()+0); // pop wahi begin se leke + 0 tak generate(ip,notpick); generate(ip,pick); } vector subsetsWithDup(vector& nums) { vector op; generate(nums,op); vector finalans; //return ko vector of vectror me karana hai to ... for(auto it : ans) //set se final ans me transfer { finalans.push_back(it); } return finalans; } };
@palakgurudev4867
@palakgurudev4867 Жыл бұрын
can u please explain why are we using sorting ..it wasnt mentioned in the question right??
@vikashkumarchaurasia1299
@vikashkumarchaurasia1299 4 жыл бұрын
Bhai backtracking ka playlist placement session se pahle aajayega ?
@AbhishekChoudharyB
@AbhishekChoudharyB Жыл бұрын
*Unique subsequences vs unique combinations/subsets* Suppose the original given string is abab Then the total subsequences will be void, a, b, a, b, ab, aa, ab, ba, bb, ab, aba, abb, aab, bab, abab Now the unique subsequences will be void, a, b, ab, aa, ba, bb, aba, abb, aab, bab, abab But the number of unique combinations (or unique subsets) will be less than unique subsequences The unique combinations here will be void, a, b, ab=ba, aa, bb, aba=aab, abb=bab, abab(=aabb) To find the unique combinations we can sort the string abab first to aabb Then the resulting unique subsequences will unique combinations void, a, b, aa, ab, bb, aab, abb, aabb
@snehilsinha4689
@snehilsinha4689 3 жыл бұрын
7:23 Don't know about right but I think I can easily left the code 😆. jk bhaiya. OP content 🔥🔥🔥
@shaikfahim1232
@shaikfahim1232 3 жыл бұрын
Bhayya 80k subscribers hogaye hain Please abseto graphs start kardijiye Lockdown me utna bhi mushkil nahi hoga
@aryankanwar8137
@aryankanwar8137 Ай бұрын
Solve using a previous similar approach without using extra space 1. Add index instead of slicing input 2. Using sort (so duplicates come subsequent 1,2,2,3 and while to eliminate duplicate var subsetsWithDup = function(nums) { nums.sort((a, b) => a - b); let powerset = []; let n = nums.length; function solve(index, output = []){ if(index == nums.length){ powerset.push(output); return; } let output1 = output.slice(); let output2 = output.slice(); output2.push(nums[index]); solve(index+1, output2); //include in output // Skip all duplicate elements for exclude case // we took one 2 and here we want to exclude it, so we will exclude not just single 2 but all subsequent 2's while(index + 1 < nums.length && nums[index] === nums[index + 1]) { index++; } solve(index+1, output1); //exclude in output } solve(0); return powerset; };
@nikhilchhabra4564
@nikhilchhabra4564 2 жыл бұрын
BEST EXPLANATION EVER
@sagarkanojia
@sagarkanojia 4 жыл бұрын
Bhai isse bhadiya content aur kahin nahi mila aajtak
@NotNotNithin
@NotNotNithin 4 жыл бұрын
Java 8 solution for printing unique subsets: public class PrintUniqueSubsets { public static void main(String[] args) { Set uniqueSubset = new HashSet(); uniqueSubset = printUnique("aab", "", uniqueSubset); System.out.println("Unique subsets: "); uniqueSubset.forEach(System.out::println); } public static Set printUnique(String ipStr, String opStr, Set resMap) { if (ipStr.isEmpty()) { resMap.add(opStr); return new HashSet(); } String opStr2 = opStr; opStr2 += ipStr.charAt(0); ipStr = ipStr.substring(1); printUnique(ipStr, opStr, resMap); printUnique(ipStr, opStr2, resMap); return resMap; } }
@vinitdeshkar4306
@vinitdeshkar4306 11 ай бұрын
This wont work for input like "aaba". Your approach will still print duplicate subsets.
@sagardas4569
@sagardas4569 4 жыл бұрын
Sachme bhaiya maja agaya itna details me janne k baad 😇😘
@056_anmolkumarojha2
@056_anmolkumarojha2 3 жыл бұрын
the content is heaven for people like me who struggled in recursion, but till now we were familiar with IBH method is there any type of corelation between this rec tree method and IBH one if anyone could tell it would be of great help to memorise it
@paritoshdadhich2954
@paritoshdadhich2954 3 жыл бұрын
Thanks a lot for such a great explanation and generalize all type of quesitons
@Raj10185
@Raj10185 Жыл бұрын
solution for leetcode subsets :- class Solution { public: vector ans ; //global ans bana liya asseseble for all void generate(vector ip,vector op) { if(ip.size()==0) { ans.push_back(op); return; } // int pick ke jgh ab vector hoga vector notpick = op; vector pick = op ; pick.push_back(ip[0]); // ab push_back ip[0] mtlb 1 row wala pura aa jyega ip.erase(ip.begin()+0); // pop wahi begin se leke + 0 tak generate(ip,notpick); generate(ip,pick); } vector subsets(vector& nums) { vectorop; generate(nums,op); return ans; } };
@paragroy5359
@paragroy5359 3 жыл бұрын
Thanks for the playlist sir....your work is extremely good
@Fakipo
@Fakipo 3 жыл бұрын
We can also use set in the place of map as it will be more efficient. Around 18:30, you mentioned that in a subset order doesn't matter, while it is the other way around. Order matters, that's why "ac" is different from "ca". Also important point to mention, in case of substrings and subsequences, you need to remove some cases from the last level of the tree where input is empty string. and then build your induction around that.
@nishantgupta6902
@nishantgupta6902 Жыл бұрын
code: public: set s; void solve(vector ip, vector op){ if(ip.size() ==0){ s.insert(op); return; } vector op1 = op; vector op2 = op; op2.push_back(ip[0]); ip.erase(ip.begin() + 0); solve(ip,op1); solve(ip,op2); return; } //Function to find all possible unique subsets. vector AllSubsets(vector arr, int n) { // code here sort(arr.begin(), arr.end()); vector op; vector ans; solve(arr, op); for(auto i: s){ ans.push_back(i); } return ans; }
@bhooveshvyas2186
@bhooveshvyas2186 7 ай бұрын
brother it is working but giving TLE.
@sagardhandhalya2067
@sagardhandhalya2067 4 жыл бұрын
You are wonderful teacher 👏👏
@chetanraghavv
@chetanraghavv 9 ай бұрын
In the first approach, instead of creating another vector and using map, I think we can just store all the elements in unordered_set which has amortized O(1) for insert and at the end we can just copy all the elements from our set to answer vector
@ashirbadbehera5544
@ashirbadbehera5544 4 жыл бұрын
22:42 set is a better option here.
@iam_mausam
@iam_mausam 2 жыл бұрын
11:28 Nice Drawing : )
@utkarshsharma6650
@utkarshsharma6650 3 жыл бұрын
bhai coding nahi karwayi apne lekin, concept kafi accha karwaya! maza aagya bhai ! crash course type ke liye acchi series h
@utkarshsharma6650
@utkarshsharma6650 3 жыл бұрын
bhai aapne code nhi dia, ye bahut galat kia,. kam se kam code toh de dete... bharat achrya wali strategy laga di, adha content dedia baki ke liye fasa dia :-(
@puspaksahu3342
@puspaksahu3342 3 жыл бұрын
how to print all the subsets? how can we do the same as subsequece, as we know both are different?? there are 2^n subsequences possible while n(n+1)/2 substrings possible
@kumaarbalbir1023
@kumaarbalbir1023 3 жыл бұрын
thanks for info i was somehow confused about subsequence
@vasuagrawal2954
@vasuagrawal2954 3 жыл бұрын
oo bhai its her choice 🔥🔥
@yashmittal9040
@yashmittal9040 4 жыл бұрын
hi sir i think you missed saying that if the input string with duplicates is not in sorted order then it must be sorted first before finding the subsets
@pankajbisht897
@pankajbisht897 3 жыл бұрын
It will be tricky to solve it if the input is List and the output is
@prasannpatil3371
@prasannpatil3371 3 жыл бұрын
I understood that you formed new instances as lists are mutable and change in place unlike string. But how did you decide to form new instance here "var op1 = new List (output);" and not here " result.Add (output)". If yu could please explain this I would be greateful
@prasannpatil3371
@prasannpatil3371 3 жыл бұрын
@Pankaj Bisht
@shibanidas7018
@shibanidas7018 3 жыл бұрын
Thanks!! helped a lot
@shaiknadeen
@shaiknadeen Жыл бұрын
function withoutduplicates(idx, arr, ans, dummy) { ans.push(dummy.slice()); for (let i = idx; i < arr.length; i++) { if(i>idx&&arr[i]==arr[i-1]){ continue; } dummy.push(arr[i]); withoutduplicates(i+1, arr, ans, dummy); dummy.pop(); } } js code simple
@mohammad_5929
@mohammad_5929 2 жыл бұрын
it's her choice 😂😂bhayya roaster
@ShubhamSingh-fl3qi
@ShubhamSingh-fl3qi 4 жыл бұрын
Agar koi larki kre to it's her choice 😂😂nice one
@swagatochakraborty6043
@swagatochakraborty6043 Жыл бұрын
11:00 Substring vs Subsequence vs Subset
@PriyankaRawat-be4sq
@PriyankaRawat-be4sq 3 жыл бұрын
can we somehow do this problem without using extra data structure to filter out the unique? in recusrion itself if we can have some condition to not check for duplicates?
@anchalsharma0843
@anchalsharma0843 10 ай бұрын
yes , we can do that. Sort the array first and then dont recurse for an element if you had already done it ( sorting the input arr can help us to determine quickly if we had recursed for a similar element earlier or not )
@pranayreddyannam188
@pranayreddyannam188 2 жыл бұрын
Unique Subsets | Practice | GeeksforGeeks for this question, when we are erasing first element of input till it become zero, As we know erasing at first index will costs O(n) time, so is there any other way @Aditya Verma to reduce the time complexity, like in discussions I have seen some are doing without erasing the input
@rahuldwarijadavpuruniversi9322
@rahuldwarijadavpuruniversi9322 Жыл бұрын
class Solution { public: vectorres; void solve(vectornums,vector&op){ if(nums.size() == 0){ res.push_back(op); return; } vectorop1 = op; vectorop2 = op; op2.push_back(nums[nums.size() - 1]); nums.pop_back(); solve(nums,op1); solve(nums,op2); } vector subsets(vector& nums) { vectorop; solve(nums,op); return res; } }; start from the last element and use pop_back() which will cost you O(1) time only
@sahinsarkar7293
@sahinsarkar7293 Жыл бұрын
in 18:35, did you mean print "subsequence" in both cases, instead of "subset"?
@ArshadIqbal_IEC_
@ArshadIqbal_IEC_ 25 күн бұрын
exactly looking for this comment
@abhinavrana221
@abhinavrana221 3 жыл бұрын
Simple C++ Code I have used Set #include using namespace std; string solve(string str,string op,set&st){ if(str.length()==0){ st.insert(op); return op; } string op1=op; string op2=op; op2.push_back(str[0]); str.erase(str.begin()+0); solve(str,op1,st); solve(str,op2,st); return op; } // c a ac a ac aa aac int main(){ string str; cin>>str; string op=" "; setst; solve(str,op,st); for (auto it = st.begin(); it !=st.end(); ++it) cout
@kumarmanish9046
@kumarmanish9046 3 жыл бұрын
How can we use subset solution for finding powerset, subset, subsequence? They would have different results right? Example if answer demands ab & ba both, then?
@kishanmishra9802
@kishanmishra9802 4 жыл бұрын
Great teacher
@srishtigugoriya9319
@srishtigugoriya9319 2 жыл бұрын
can anyone please share their java code which got accepted on GFG
@siddharthkeshri5649
@siddharthkeshri5649 3 жыл бұрын
17:15 Aditya bhaiya is Also a Neha Dupia Fan 😂😂
@dhananjaylohiya6421
@dhananjaylohiya6421 Жыл бұрын
Baaki sbka thik hai , smjh gye ek taraf bhai ka "COOL" taraf
@animeshanand6286
@animeshanand6286 4 жыл бұрын
waah waah. nice reference through memes. makes the concepts solid
@sagnik_20
@sagnik_20 3 жыл бұрын
unordered_set use karsakte hae na map k jagah
@spandanbasu_1228
@spandanbasu_1228 2 жыл бұрын
🤣🤣🤣Its her Choice.. Neha Dhupia shoud be proud!!😆😆
@chiefolk
@chiefolk 2 жыл бұрын
we can use unordered_set instead of map and a vector and then create a iterator for unordered_set to print unique items
@amruthachowdary5697
@amruthachowdary5697 4 жыл бұрын
Any pls share the c++ code of this problem
@nitingupta1417
@nitingupta1417 4 жыл бұрын
#include using namespace std; void subset(string str,string output,vector &v) { if(str.length()==0) { v.push_back(output); return; } string op1=output; string op2=output+str[0]; str.erase(str.begin()); subset(str,op1,v); subset(str,op2,v); } int main() { string str,output=" "; cin>>str; vector v; subset(str,output,v); vector unique; map m; for(int i=0;i
@abhisheksuryavanshi9675
@abhisheksuryavanshi9675 4 жыл бұрын
Thanks @@@nitingupta1417
@abhayrai1724
@abhayrai1724 3 жыл бұрын
@@nitingupta1417 so nice of you :-)
@nitingupta1417
@nitingupta1417 3 жыл бұрын
@@abhisheksuryavanshi9675 welcome 😇
@nitingupta1417
@nitingupta1417 3 жыл бұрын
@@abhayrai1724 welcome bro 😇😇
@ShivamKumar-cv7jv
@ShivamKumar-cv7jv 4 жыл бұрын
so much addictive videoes...........
@ytg6663
@ytg6663 3 жыл бұрын
17:15 Neha Dhupiya Meme 👍😁😁
@ritwikgoyal1659
@ritwikgoyal1659 3 жыл бұрын
Any tips how to solve questions , i feel like i am not making any progress
@nitindas208
@nitindas208 4 жыл бұрын
problem link : practice.geeksforgeeks.org/problems/subsets/0
@jivanninawe3190
@jivanninawe3190 3 жыл бұрын
Kohi code send kar sakta hai ky muje unique subset string ka?
@_ApoorvaShinde
@_ApoorvaShinde 4 жыл бұрын
best video ever!!
@Neerajkumar-xl9kx
@Neerajkumar-xl9kx 4 жыл бұрын
what is mean by in place string ??
@yatendraupadhyay2180
@yatendraupadhyay2180 9 ай бұрын
Bhaiya ye to sirf subsequence hua na . What about all subsets.
@chiraggandhi8115
@chiraggandhi8115 4 жыл бұрын
Will the above map thing work for test case : 4,1,4,4,4
@JatinKapoor7
@JatinKapoor7 4 жыл бұрын
nope will not work for above
@JatinKapoor7
@JatinKapoor7 4 жыл бұрын
sort input string before sending to solve will fix it :) sort( nums.begin(), nums.end());
@priyankarai7005
@priyankarai7005 4 жыл бұрын
program to print all substrings recursively. Please help me with it.
@tejasjhamnani7724
@tejasjhamnani7724 3 жыл бұрын
// Program to print all substrings Recursively #include #include #include using namespace std; void solve(string output, string input, set &arr, int selected) { if (input.length() == 0) { arr.insert(output); return; } string op1 = output; string op2 = output; op1.push_back(input[0]); input.erase(input.begin()); if (selected == 1) { solve(op1, input, arr, 1); } solve(op2, input, arr, 0); } int main() { set arr; int selected = 1; string input = "", output = "abcdefg"; //Initial Input and output Respectively solve(input, output, arr, selected); set::iterator itr; for (itr = arr.begin(); itr != arr.end(); itr++) { cout
@anshumansharma4580
@anshumansharma4580 3 жыл бұрын
void sub(string ip, string op, bool path, int prev){ if(ip.size()==0){ cout
@jivanninawe3190
@jivanninawe3190 3 жыл бұрын
@@tejasjhamnani7724 this substring of subsets unique?
@samikshagupta78
@samikshagupta78 3 жыл бұрын
The pdf notes are all paid na?? PLEASE HELP
@kuldeepnarayanminj
@kuldeepnarayanminj 4 жыл бұрын
laajawaab, kya padhate ho aditya sir
@aayushpagare9366
@aayushpagare9366 4 жыл бұрын
set wali method TLE de deti hai large inputs par :
@aniketbhoite7168
@aniketbhoite7168 3 жыл бұрын
unordered_map
@thewanderingrooh
@thewanderingrooh 3 жыл бұрын
instead of using a set/map to remove duplicates , we can sort the input string and skip same consecutive elements.
@SDE_FACT
@SDE_FACT 2 жыл бұрын
That would give incorrect answer eg: Dry run your approach for "aab" [HINT: Your approach would probably skip "aa", but that must be present]
@kashishsingh3561
@kashishsingh3561 4 жыл бұрын
GETTING TLE in GFG in this code: (for PRINT UNIQUE SUBSETS) void solve(vector ip, vector op, vector& v) { if(ip.size()==0) { v.push_back(op); return; } vector op1=op; vector op2=op; op2.push_back(ip[0]); ip.erase(ip.begin() + 0); solve(ip, op1, v); solve(ip, op2, v); return; } vector AllSubsets(vector A, int n) { // code here vector v; vector op={}; sort(A.begin(), A.end()); solve(A, op, v); set s; int size = v.size(); for(int i = 0; i < size; ++i ) s.insert(v[i]); v.assign( s.begin(), s.end() ); return v; }
@prakhar1144
@prakhar1144 2 жыл бұрын
Try this :) struct VectorHasher { size_t operator()(const vector& V) const { size_t hash = V.size(); for (auto i : V) hash ^= i + 0x9e3779b9 + (hash > 2); return hash; } }; class Solution { public: void solve(vector ip, vector op, unordered_set& ans) { if(ip.size()==0) { ans.insert(op); return; } vector op1 = op; vector op2 = op; op2.push_back(ip[0]); ip.erase(ip.begin()+0); solve(ip, op1, ans); solve(ip, op2, ans); } //Function to find all possible unique subsets. vector AllSubsets(vector arr, int n) { sort(arr.begin(), arr.end()); unordered_set ans; vector ans_vec; vector ip = arr; vector op; solve(ip, op, ans); for(auto x : ans) { ans_vec.push_back(x); } sort(ans_vec.begin(), ans_vec.end()); return ans_vec; } };
@chakravarty-with-a-v
@chakravarty-with-a-v 2 жыл бұрын
Try Passing the vectorop in solve() by reference i.e. void solve(vector ip, vector& op, vector& v)
@spiral546
@spiral546 2 жыл бұрын
@@chakravarty-with-a-v How does by passing reference worked?
@chakravarty-with-a-v
@chakravarty-with-a-v 2 жыл бұрын
@@spiral546 passing by reference will not create a copy. Function will work on the original data. If you pass by value then copies will be generated. This will cause a problem for large inputs because copying is done repeatedly taking memory.
@yuvrajkakran1101
@yuvrajkakran1101 Жыл бұрын
void solve(vectorin,vectorop,set& ans){ if(in.size()==0){ sort(op.begin(),op.end()); ans.insert(op); return; } vectorop1=op,op2=op; op2.push_back(in[0]); in.erase(in.begin()+0); solve(in,op1,ans); solve(in,op2,ans); } //each mother fucking subset should be sorted vector AllSubsets(vector in, int n) { vectorans1; setans; vector op; solve(in,op,ans); ans1.assign( ans.begin(), ans.end() ); return ans1; }
@azizvohra3142
@azizvohra3142 2 жыл бұрын
Guys can anyone update here with the latest code for this problem on leetcode using hashmap and the same procedure which Aditya did?
@prashantangrish7817
@prashantangrish7817 2 жыл бұрын
Hi Aditya , please make backtracking videos as well
@parvahuja7618
@parvahuja7618 8 ай бұрын
thankyou sir so much
@mshreya9
@mshreya9 4 жыл бұрын
Hi, please share the JAVA code for the problem. Thanks!
@2010aishwary
@2010aishwary 4 жыл бұрын
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Subsets { public static void main(String[] args) { solveSubsets("abc", ""); } static void solveSubsets(String input, String output){ List inputList = new ArrayList(); if(input.length() == 0){ inputList.add(output); System.out.println(inputList); return; } solveSubsets(input.substring(1), output); solveSubsets(input.substring(1), output+input.charAt(0)); return; } }
@DINESHKUMAR-gw1kz
@DINESHKUMAR-gw1kz 4 жыл бұрын
@@farazahmad9734 how to do this with Arraylist instead of string ?
@amanshaw8496
@amanshaw8496 4 жыл бұрын
@@DINESHKUMAR-gw1kz wo sirf ldki ko btayega 😂
@Education_2753
@Education_2753 4 жыл бұрын
@@amanshaw8496 epic reply bro 😂😂
@akhileshshahare
@akhileshshahare 2 жыл бұрын
const arr = [1,2,3] const solve = (ip, op, res) => { if(ip.length === 0){ res.push(op) return } let op1 = op, op2 = op op2.push(ip[0]) ip.shift() solve(ip, op1, res) solve(ip, op2, res) } let op = [], res = [] solve(arr, op, res) console.log(res) Why this approach for numbers is not working 😕?
@aayush5474
@aayush5474 4 жыл бұрын
why not jut put the subset in a ordered set and print the set
@glean56
@glean56 3 жыл бұрын
it'll be great if you can mention the links to the other videos you reference in a video, thanks
@shubhammahindru3563
@shubhammahindru3563 Жыл бұрын
Its her choice 😁😁
@sparsharora6516
@sparsharora6516 3 жыл бұрын
neha dhupia ko recursion aata hai?
@ROHITKADYANJI
@ROHITKADYANJI 3 жыл бұрын
Bhai ek interaction video rakh lo
@rishabsharma5307
@rishabsharma5307 9 ай бұрын
Simple C++ solution: ``` set ms; void findSubsets(vector &nums, int n, vector temp) { if(n == 0) { ms.insert(temp); return; } findSubsets(nums, n-1, temp); temp.push_back(nums[n-1]); findSubsets(nums, n-1, temp); } vector subsetsWithDup(vector& nums) { sort(nums.begin(), nums.end()); findSubsets(nums, nums.size(), {}); vector result; for(auto &x : ms) result.push_back(x); return result; } ```
@deyp11
@deyp11 4 жыл бұрын
thanks a lot
@Prateek_Mantry
@Prateek_Mantry 9 ай бұрын
thank you.
@accepted5856
@accepted5856 4 жыл бұрын
But our only choice for learning is Aditya verma
@siddhartharauta7483
@siddhartharauta7483 4 жыл бұрын
Adity bhai is string me 3 elements leke batado na keisa hoga ...just explain kardo ham code karlenge..... Ye 3 elements keliye Kruse tree banau samajh me nehi aa raha
@rafiqn2675
@rafiqn2675 4 жыл бұрын
Thank you...
@vickyjha4509
@vickyjha4509 8 ай бұрын
#include using namespace std; vector combi(string ); int main() { string input; cin>>input; vector ans=combi(input); for(auto x:ans) cout
@pratideep-
@pratideep- Жыл бұрын
GFG solution. ---> set st; void solve(vector sub, vector arr, int idx) { if (idx >= arr.size()) { sort(sub.begin(), sub.end()); st.insert(sub); return; } solve(sub, arr, idx + 1); sub.push_back(arr[idx]); solve(sub, arr, idx + 1); } vector AllSubsets(vector arr, int n) { vector ans; vector sub; solve(sub, arr, 0); for (auto i : st) { ans.push_back(i); } return ans; }
@SILENT_4_M_6551
@SILENT_4_M_6551 Жыл бұрын
could you tell me please , why u are sorting the sub?Brother
@drashtibhardwaj50
@drashtibhardwaj50 3 жыл бұрын
It's her choice 😂😂😂
@mahavirsingh5790
@mahavirsingh5790 4 жыл бұрын
Bhaiya backtracking ki video bna do plz placement time aane wala hai
@Krishna-mz2uk
@Krishna-mz2uk Жыл бұрын
Java Version for this approach class Solution { List res; Set ans; void solve(ArrayList ip,ArrayList op) { if(ip.size()==0) { if(ans.contains(op)==false){ ans.add(op); res.add(op); } return; } ArrayList op1=(ArrayList)op.clone(); ArrayList op2=(ArrayList)op.clone(); ArrayList ip1=(ArrayList)ip.clone(); op2.add(ip.get(0)); ip1.remove(0); solve(ip1,op1); solve(ip1,op2); return; } public List subsetsWithDup(int[] nums) { Arrays.sort(nums); res=new ArrayList(); ans=new HashSet(); ArrayList ip=new ArrayList(); Arrays.stream(nums).forEach(ip::add); ArrayList op=new ArrayList(); solve(ip,op); return res; } }
@aneesmd7837
@aneesmd7837 3 жыл бұрын
This approach is giving TLE in gfg
@prakhar1144
@prakhar1144 2 жыл бұрын
struct VectorHasher { size_t operator()(const vector& V) const { size_t hash = V.size(); for (auto i : V) hash ^= i + 0x9e3779b9 + (hash > 2); return hash; } }; class Solution { public: void solve(vector ip, vector op, unordered_set& ans) { if(ip.size()==0) { ans.insert(op); return; } vector op1 = op; vector op2 = op; op2.push_back(ip[0]); ip.erase(ip.begin()+0); solve(ip, op1, ans); solve(ip, op2, ans); } //Function to find all possible unique subsets. vector AllSubsets(vector arr, int n) { sort(arr.begin(), arr.end()); unordered_set ans; vector ans_vec; vector ip = arr; vector op; solve(ip, op, ans); for(auto x : ans) { ans_vec.push_back(x); } sort(ans_vec.begin(), ans_vec.end()); return ans_vec; } };
Permutation with spaces
21:11
Aditya Verma
Рет қаралды 90 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Generate all Balanced Parentheses
34:03
Aditya Verma
Рет қаралды 153 М.
Print Subsets | Print PowerSets | Print all Subsequences
15:48
Aditya Verma
Рет қаралды 198 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 231 М.
Subsets - Leetcode 78 - Recursive Backtracking (Python)
11:51
Greg Hogg
Рет қаралды 13 М.
The Doomsday Algorithm - Numberphile
14:33
Numberphile
Рет қаралды 858 М.
What P vs NP is actually about
17:58
Polylog
Рет қаралды 147 М.
How to Remember Everything You Read
26:12
Justin Sung
Рет қаралды 3,1 МЛН
Subsets II - Backtracking - Leetcode 90 - Python
15:05
NeetCode
Рет қаралды 125 М.
Tower of Hanoi | Recursion
24:01
Aditya Verma
Рет қаралды 176 М.
Kth Symbol in Grammar
23:32
Aditya Verma
Рет қаралды 127 М.