8 Permutation of Strings | Backtracking Solution

Поділитися
Вставка
  • Опубліковано 11 гру 2023
  • 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.geeksforgeeks.org/problem...
    ------------------------------------------------------------------------------------------
    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.

КОМЕНТАРІ • 51

  • @wiselife5821
    @wiselife5821 3 місяці тому +4

    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 6 місяців тому +7

    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.

  • @saumyagaur7633
    @saumyagaur7633 6 місяців тому +2

    Great video. BT never felt this easy

  • @JangBahadur3028
    @JangBahadur3028 6 місяців тому +3

    Was waiting for this videos after the last 😊

  • @prrockzed
    @prrockzed 6 місяців тому +1

    Was waiting for the video.... thanks a lot

  • @gamingbeast3555
    @gamingbeast3555 6 місяців тому

    Thanks a lot bhaiya for such great videos... bs bhaiya tree aur graph ki series aur laa do finall request bhaiya..

  • @randomsongs8835
    @randomsongs8835 5 місяців тому

    Hello Sir, Please complete this series. It is really helpful

  • @anutoshghosh7893
    @anutoshghosh7893 4 місяці тому +1

    Very well explained. So basically, here the flow is similar to DFS here right and not BFS, just wanted to clarify that?

  • @rishabhahuja7413
    @rishabhahuja7413 6 місяців тому +5

    Graphs and Trees please🙏

  • @gamingbeast3555
    @gamingbeast3555 6 місяців тому

    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 Місяць тому

    Thank you Aditya Verma Salute 🥷

  • @049bite_gauravkumarsoni3
    @049bite_gauravkumarsoni3 6 місяців тому +5

    please make a video of Trees and Graph also.. ❣

  • @kullumanali-bu8uf
    @kullumanali-bu8uf 4 місяці тому

    you're one of best

  • @TanuKansal-nn4el
    @TanuKansal-nn4el 6 місяців тому

    It's showing 2 unavailable videos. Is there any video published after permutation question ?

  • @Raj10185
    @Raj10185 6 місяців тому

    Awsome video maza aa gaya

  • @AjayThakur-gu9hg
    @AjayThakur-gu9hg 6 місяців тому

    Kindly complete the dp series... you missed some patterns mentioned in 1 st video

  • @xiaoshen194
    @xiaoshen194 6 місяців тому

    To ab main sum string wala ques kr paunga gfg pe?

  • @Prateek_Mantry
    @Prateek_Mantry 2 місяці тому +1

    thank you.

  • @aniketsharma472
    @aniketsharma472 5 місяців тому

    SIr, please complete this series.....

  • @mukulmalik6263
    @mukulmalik6263 6 місяців тому

    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

  • @ritwikvaidya5754
    @ritwikvaidya5754 3 місяці тому

    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.

  • @gamingbeast3555
    @gamingbeast3555 6 місяців тому

    Bhaiya aapsae request hai aapk tree and graph ki series aur pda do please bhaiya..

  • @sadafkausar8770
    @sadafkausar8770 6 місяців тому +2

    Hello sir, recently I completed your recursion playlist so next what should I start your backtracking playlist or your DP playlist.

  • @gurupreetsingh8347
    @gurupreetsingh8347 6 місяців тому +1

    Nice

  • @Raj10185
    @Raj10185 6 місяців тому +1

    Watched the video and successfully solved permutation 1 and 2 of the leetcode

  • @saurabhjalutharia
    @saurabhjalutharia 6 місяців тому +1

    volume thoda high m record kiya kro, full volumne m bhi low lgti h

  • @kullumanali-bu8uf
    @kullumanali-bu8uf 4 місяці тому

    this code is working even if we pass the string by value ...why??

  • @abdulmohsin1
    @abdulmohsin1 5 місяців тому

    Aditya bro, Voice thodi low hai video ki, please increase it a bit if possible, thank you so much

  • @anushakandagal1727
    @anushakandagal1727 Місяць тому

    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

  • @divyranjan254
    @divyranjan254 3 місяці тому

    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.

  • @SaurabhMishraa
    @SaurabhMishraa 6 місяців тому

    ❤❤❤❤❤❤

  • @abhi224505
    @abhi224505 3 місяці тому +1

    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 3 місяці тому

      *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

  • @kalpagarwal8291
    @kalpagarwal8291 Місяць тому

    waiting for generating unique permutations.

  • @bommakantibeeraiah1486
    @bommakantibeeraiah1486 3 місяці тому +2

    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

  • @ratneshtiwari2028
    @ratneshtiwari2028 6 місяців тому

    Subarrays with Sum ‘k' please help me with this problem'

  • @pratik9449
    @pratik9449 5 місяців тому

    15:15 main video starts here

  • @_desouvik
    @_desouvik 2 місяці тому

  • @siddarthchidurula5931
    @siddarthchidurula5931 6 місяців тому

    Trees padhlo bhai...striver ki pattern samajh nahi a raha hai

  • @sumitbasu5146
    @sumitbasu5146 Місяць тому

    Tree Tree Tree Please please please

  • @eshaansharma145
    @eshaansharma145 5 місяців тому +1

    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 4 місяці тому

      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?

  • @abhayjoshi362
    @abhayjoshi362 6 місяців тому

    ye kaisi baat kr rahi hai aap devi ji🤓

  • @ratneshtiwari2028
    @ratneshtiwari2028 6 місяців тому

    tree

  • @mukulmalik6263
    @mukulmalik6263 6 місяців тому

    Bhai itni coding ki kuch kan nhi aaya

    • @xiaoshen194
      @xiaoshen194 6 місяців тому +1

      Kyu 24 batch ka h?