Reverse Words in a String (Amazon, Microsoft, MentorGraphics, MakeMyTrip) : Explanation➕Live Coding

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ย. 2024

ความคิดเห็น •

  • @arizimam4027
    @arizimam4027 2 หลายเดือนก่อน +3

    bro's voice is so good that he has the potential to become a voice artist as well

  • @noone1000_xyz
    @noone1000_xyz 5 หลายเดือนก่อน +7

    this channel is a gem...glad i found it

  • @lucusdukes6169
    @lucusdukes6169 8 หลายเดือนก่อน +1

    thanks bhaiya very useful and easy way of understanding the concepts
    thanks a lot

  • @yashikachauhan9790
    @yashikachauhan9790 9 หลายเดือนก่อน +3

    Great explanation

  • @U2011-n7w
    @U2011-n7w ปีที่แล้ว +4

    very nice explanation bhaiya

  • @all_wood791
    @all_wood791 ปีที่แล้ว +1

    It is very helpful for everyone thanks bhaiya for provide this quality of content.

  • @anshumaan1024
    @anshumaan1024 8 หลายเดือนก่อน +1

    bhaiya , TC will be O(N*M), where N is size of given string, M is average length of words in given string 🤔🤔
    kyoki "i" pointer is also waiting for reversing character between "l" and "r", aur reverse krne me bhi toh time lagta hai, aur reverse me time depend karega ki words ki average length kitni hai

  • @queenshijain3089
    @queenshijain3089 3 หลายเดือนก่อน +1

    To be honest I know if I am able to solve questions on my coding test i somewhere know it is beacuse of u !!Thank you so much very greatful!!

  • @AMANKUMAR-ys8iy
    @AMANKUMAR-ys8iy 4 หลายเดือนก่อน +3

    string reverseWords(string s) {
    stringstream ss(s);
    string token="";
    string result="";

    while(ss>>token){
    if (!result.empty()) {
    result = token + " " + result;
    }
    else {
    result = token;
    }
    }

    return result;
    }

  • @surajsidar2294
    @surajsidar2294 7 หลายเดือนก่อน +2

    Thanks for providing solution without using utility method.
    Code in Java for approach 2
    class Solution {
    public String reverseWords(String s) {
    int left = 0;
    int right = 0;
    StringBuilder sb = new StringBuilder(s);
    reverse(0, s.length() - 1, sb);
    int n = s.length();
    int i = 0;
    while (i < n) {
    while (i < n && sb.charAt(i) != ' ') {
    sb.setCharAt(right, sb.charAt(i));
    right++;
    i++;
    }
    if (left < right) {
    reverse(left, right - 1, sb);
    if (right < n)
    sb.setCharAt(right, ' ');
    right++;
    left = right;
    }
    i++;
    }
    return sb.toString().substring(0, right - 1);
    }
    void reverse(int left, int right, StringBuilder sb) {
    while (left < right) {
    char ch = sb.charAt(left);
    sb.setCharAt(left, sb.charAt(right));
    sb.setCharAt(right, ch);
    left++;
    right--;
    }
    }
    }

    • @SenTineL00
      @SenTineL00 หลายเดือนก่อน

      Bhaiya yeh line ki kya need thi bataoge ?
      if (right < n)
      sb.setCharAt(right, ' ');

  • @vishalrai6027
    @vishalrai6027 4 หลายเดือนก่อน +1

    your explanation is fabulous. your are so consistent, bhaiya can you tell me how to stay consistent.

  • @rajkumarroul7306
    @rajkumarroul7306 ปีที่แล้ว +4

    s=s.substr(0,r-1) trims the trainling whitespaces but how is is preceeding whitespaces trimmed?? i cant understand

  • @souravjoshi2293
    @souravjoshi2293 2 ปีที่แล้ว +2

    Thanks for the clean code bro

  • @bkmeher9005
    @bkmeher9005 หลายเดือนก่อน +1

    wao

  • @jatinsood9277
    @jatinsood9277 4 หลายเดือนก่อน

    Great Explanation

  • @bhumikanaik4801
    @bhumikanaik4801 3 หลายเดือนก่อน

    Can u please explain the TC of two pointer approach.

  • @wearevacationuncoverers
    @wearevacationuncoverers ปีที่แล้ว +1

    very helpful

  • @techie5345
    @techie5345 18 วันที่ผ่านมา +1

    I solved in different way.. Like run loop from last till first get the words n append it to result so overall words will be reverse //REVIEW CODE & PLACE SUGGESTIONS
    private static String reverseWordsInStringTwoPointer(String str) {
    String result = "";
    int i = str.length() - 1;
    int first = str.length() - 1;
    int last = str.length() - 1;
    char[] stringArray = str.toCharArray();
    while (i >= 0) {
    if (stringArray[i] != ' ') {
    if (stringArray[last] == ' ') {
    last = i;
    }
    first = i;
    }
    if (stringArray[i] == ' ' && stringArray[last] != ' ') {
    result = result.concat(str.substring(first, last + 1).concat(" "));
    last = i;
    first = i;
    }
    if (i == 0 && stringArray[i] != ' ') {
    result = result.concat(str.substring(first, last + 1).concat(" "));
    }
    i--;
    }
    return result.substring(0, result.length() - 1);
    }

  • @darshandesale
    @darshandesale ปีที่แล้ว

    very very usefull for everyone thanks mik sir pehchana darshan desal linkdin pe meesege kiya tha apko

  • @sandeshjogdande8830
    @sandeshjogdande8830 4 หลายเดือนก่อน +1

    very good

  • @abhishekrai4002
    @abhishekrai4002 10 หลายเดือนก่อน

    ek number

  • @SohelKhan-vt5ql
    @SohelKhan-vt5ql ปีที่แล้ว +2

    Can you please explain how the 2nd approach takes O(1) memory? Doesn't reverse in C++ take O(N) time and O(N) space?

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +2

      Reverse will not take O(n) space. It can be done Inplace.
      For example - “abcd”
      Swap a with d -- “dbca”
      Now swap b with c - “dcba”

    • @aayushranjan5572
      @aayushranjan5572 ปีที่แล้ว

      sach me ?@@codestorywithMIK

  • @Samtoosoon
    @Samtoosoon 5 หลายเดือนก่อน +1

    two pointer se karlo,stack se pop karlo warna subproblems solve karlo recursive way se dhanyawad

  • @sunnyvlogs__
    @sunnyvlogs__ 2 ปีที่แล้ว +1

    Bhaiya if( l < r) yeh condition bas normally nahi laga mere khyal se
    agar 3-4 spaces lagatar ho ussey tackle karne ke liye bhi use hoga.

    • @codestorywithMIK
      @codestorywithMIK  2 ปีที่แล้ว

      Yes. Would be good to try with an example

    • @sunnyvlogs__
      @sunnyvlogs__ 2 ปีที่แล้ว +1

      @@codestorywithMIK Yes Bhaiya...
      Thanks for understanding the solution in simple and easy way😊

  • @ankitagupta7911
    @ankitagupta7911 2 ปีที่แล้ว +3

    I didn't understand line no 21
    Please explain

    • @codestorywithMIK
      @codestorywithMIK  2 ปีที่แล้ว +6

      Let's understand it with a simple example :
      String str = "we will evlos shtam"
      Suppose in the above string I ask you to only reverse "evlos" part and make it "solve"
      For that you need the position of "e" and "s" of "evlos"
      so, let's say index of "e" is = l
      and index of "s" is = r
      Because, the function reverse(beginning_position, end_position) requires the beginning and end of something that you want to reverse.
      so position for "e" will be = str.begin() + l;
      and position of "s" will be = str.begin() + r;
      So, we pass these two positions to reverse() function
      Then it will only reverse "evlos" and make it "solve"
      So our str will now look like = "we will solve shtam"
      I hope that helps you and I was able to explain it well.
      Let me know if there is any further doubt.
      Thanks a lot for watching 😇

    • @ankitagupta7911
      @ankitagupta7911 2 ปีที่แล้ว +2

      Tqsm for this explanation

    • @ayushuniyal2586
      @ayushuniyal2586 ปีที่แล้ว

      ,

  • @prajwalshaw9217
    @prajwalshaw9217 ปีที่แล้ว +1

    Hello sir ....I had a question. Will an interviewer all us to use inbuilt reverse stl during an interview?

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว

      It totally depends.
      You can ask the interviewer if you can use Stl or not.
      If they ask to implement your own, then you will have to implement.

  • @allmovies2473
    @allmovies2473 4 หลายเดือนก่อน +1

    Aapke leetcode mai dark theme nhi hai kya?

    • @codestorywithMIK
      @codestorywithMIK  4 หลายเดือนก่อน

      In my current videos, i now use dark theme ❤️

  • @ankitagupta7911
    @ankitagupta7911 2 ปีที่แล้ว +1

    class Solution {
    public:
    string reverseWords(string s) {
    stringstream str(s);
    string word, result = "";
    int n = s.length();
    while(str >> word)
    {
    result = word + " " + result;
    }
    return result.substr(0,n-1);
    }
    };
    Approch 1 I'm getting wrong answer
    please help

    • @codestorywithMIK
      @codestorywithMIK  2 ปีที่แล้ว +3

      n is the length of the input string s.
      Your resultant string has a different length.
      So in the last line just replace n with result.length()

    • @AMANKUMAR-ys8iy
      @AMANKUMAR-ys8iy 4 หลายเดือนก่อน

      @@codestorywithMIK while(ss>>token){
      if (!result.empty()) {
      result = token + " " + result;
      }
      else {
      result = token;
      }
      }

  • @vaibhavrajput8677
    @vaibhavrajput8677 ปีที่แล้ว

    Time complexity of this code 0(n^2) ?

    • @rajkumarroul7306
      @rajkumarroul7306 ปีที่แล้ว

      no its O(N)

    • @rajkumarroul7306
      @rajkumarroul7306 8 หลายเดือนก่อน

      @@anshumaan1024 time complexity of swapping two variables is O(1) because we create a temp variable and shuffle the values among the two variables and temp. Now reversing will swap characters from each corresponding ends ie forward and reverse until we reach middle so O(1) xO(N/2) which is O(N/2) which is basically O(N) and not O(N²)

    • @anshumaan1024
      @anshumaan1024 8 หลายเดือนก่อน

      @@rajkumarroul7306 TC will be O(N*M), where N is size of given string, M is average length of words in given string :), now i have understood it

    • @rajkumarroul7306
      @rajkumarroul7306 8 หลายเดือนก่อน

      @@anshumaan1024 no it will be O(N) only assume you are reversing a 11 letter sentence which has 2 ,5 letter words time complexity of reversing the whole would be O(11) and reversing each words would be O(5+5+1) ;1 is for traversing space.Bascally O(11) for reversing the whole sentence and O(11) for reversing each words which is O(N)+O(N) or O(2N ) or which is simplified O(N)

    • @rajkumarroul7306
      @rajkumarroul7306 8 หลายเดือนก่อน

      @@anshumaan1024 you are reversing each words after u have reversed the whole sentence and not that you are reversing each words after each swap/reverse of sentence so O(N)+O(N) =O(N) and not O(N*M)

  • @eshuthakur2982
    @eshuthakur2982 5 หลายเดือนก่อน

    s[r]=' ' ku kra