String Compression | Amazon | Snapchat | Microsoft | Leetcode 443

Поделиться
HTML-код
  • Опубликовано: 29 янв 2025

Комментарии • 67

  • @PrinceAdom
    @PrinceAdom 10 месяцев назад +58

    i do not understand this language (hindi?) but the illustrations were enough for me to follow through!. thank you! (dhanyavad?)

    • @codestorywithMIK
      @codestorywithMIK  10 месяцев назад +5

      Most Welcome Adom ❤️

    • @Akshay10220
      @Akshay10220 6 месяцев назад +1

      @@codestorywithMIK same problem can you try in english

  • @sarfraznawaz6379
    @sarfraznawaz6379 2 года назад +13

    Found your channel randomly and ur explanations are amazing!! I hope you reach great levels 👊🏻

    • @codestorywithMIK
      @codestorywithMIK  2 года назад +1

      Thanks a lot ♥️♥️♥️

    • @Mohit-fe5hx
      @Mohit-fe5hx 8 месяцев назад

      @@codestorywithMIK why curr_char is not compared with chars[i+1] ??? please ans

  • @hameedmulani21
    @hameedmulani21 Год назад +5

    This is so beautifully explained. Loved your way. Thank you so much for this!!

  • @Gurshxns
    @Gurshxns 7 месяцев назад +2

    loved how calm and composed you were during the explanation

  • @yashpaunikar671
    @yashpaunikar671 8 месяцев назад +6

    finally!! i found the great explaination from great teacher. thank you sir. And this is my frist problem from your string playlist. hope i will cover the whole 39 problems from this playlist. thanks again sir😍😍🙏

  • @wearevacationuncoverers
    @wearevacationuncoverers Год назад +1

    This is so beautifully explained. Loved your way. Thank you so much for this.

  • @souravjoshi2293
    @souravjoshi2293 Год назад +2

    Crystal clear explanation bro. Thanks a lot

  • @pavankumareluri9226
    @pavankumareluri9226 2 года назад +4

    nice videos broo.. too good

  • @thaman701
    @thaman701 11 месяцев назад +2

    Nicely explained bro...🎉❤.

  • @JJ-tp2dd
    @JJ-tp2dd Год назад +7

    Welcome back bhai!!! Below is the Java implementation:
    class Solution {
    public int compress(char[] chars) {
    int n = chars.length;
    int index = 0;
    int i =0;
    while(i < n) {
    char curr_char = chars[i];
    int count = 0;
    while(i < n && chars[i] == curr_char) {
    i++;
    count++;
    }
    chars[index++] = curr_char;
    if(count > 1) {
    String count_str = String.valueOf(count);
    for(char ch : count_str.toCharArray()) {
    chars[index] = ch;
    index++;
    }
    }
    }
    return index;
    }
    }

  • @vinayjoshi8714
    @vinayjoshi8714 7 месяцев назад +1

    Amazon explanation bhaiya 🥰

  • @vishal-sr5et
    @vishal-sr5et Год назад

    As usual ,bhaiya rocked

  • @SaurabhGupta-qx8hs
    @SaurabhGupta-qx8hs Год назад

    Indeed! Great Explaination

  • @nathtanmay
    @nathtanmay 4 месяца назад

    Great explanation.

  • @vishalshivkumar9936
    @vishalshivkumar9936 5 месяцев назад +1

    amazing dude

  • @nagmakhan3165
    @nagmakhan3165 Год назад

    Perfect explanation

  • @vartikatiwari5203
    @vartikatiwari5203 2 года назад +2

    samjhate aap bhut accha ho par is video me aur confusion ho gayi

    • @codestorywithMIK
      @codestorywithMIK  2 года назад

      Hi Vartika, I really appreciate your feedback. Can you please mention where the confusion came. I will try my best to fix that next time
      Thanks again

  • @biswarupacharjya2258
    @biswarupacharjya2258 Год назад +1

    super approach

  • @Bishnupriyasahoo-ud4yo
    @Bishnupriyasahoo-ud4yo 7 месяцев назад

    Thank you sir ❤

  • @tutuimam3381
    @tutuimam3381 Год назад +1

    Thanks a lot🌹

  • @bliss_anand
    @bliss_anand 5 месяцев назад

    do you think the code can be optimized by using ordered map and then just storing it in the array?

  • @pritishpattnaik4674
    @pritishpattnaik4674 Год назад

    I am trying this one of my own first , then I will post my solution then I will see your solution

  • @rhlpanwar
    @rhlpanwar 4 месяца назад

    thanks

  • @bashashaik4400
    @bashashaik4400 8 месяцев назад +1

    Got curious after seeing your reel in Instragram someone thanking you for your amazing explanation. You are doing Great Job , so beautifully , crystal clear explanation. Thank you ,❤

  • @knight-z1x
    @knight-z1x 3 месяца назад

    Hello bhaiya!!
    bhaiya,yeh question me thodi dikkit a rhi string conversion me toh isko bta dijiyega .
    3280. Convert Date to Binary

  • @sruthimajhi5610
    @sruthimajhi5610 4 месяца назад

    What is the space complexity of this code?

  • @Aman-dh9wz
    @Aman-dh9wz 7 месяцев назад

    Bhaiya approach to thik but mai java me code run karta hu .
    acc to your concept if i run then it print wrong .

  • @DebikaChatterjee-b7b
    @DebikaChatterjee-b7b 9 месяцев назад

    How to write the c and python program by using this approach?
    Actually I am facing some difficulties

  • @jagadeeshp1163
    @jagadeeshp1163 Год назад

    def compress(self, chars: List[str]) -> int:
    temp=chars[0]
    count=1
    ans=[]
    start=0
    for i in range(1,len(chars)):
    if temp==chars[i]:
    count+=1
    elif temp!=chars[i]:
    ans.append(temp+str(count))
    chars[start]=temp
    start+=1
    if count>1:
    t=str(count)
    for k in range(len(t)):
    chars[start]=t[k]
    start+=1
    count=1
    temp=chars[i]
    chars[start]=temp
    start+=1
    if count>1:
    t=str(count)
    for k in range(len(t)):
    chars[start]=t[k]
    start+=1
    return start
    Done✅

  • @kumarsaurabh5449
    @kumarsaurabh5449 2 года назад +1

    sir please tell if i want to print the string then how to print
    gfg problem

    • @kumarsaurabh5449
      @kumarsaurabh5449 2 года назад

      string encode(string src)
      {
      //Your code here
      int n=src.size();
      int indx=0;
      string ans;
      int i=0;
      while(i

    • @codestorywithMIK
      @codestorywithMIK  2 года назад +1

      bool isDigit(string ch) {
      if(ch == "0" || ch == "1" || ch == "2" || ch == "3" || ch == "4" || ch == "5"
      || ch == "6" || ch == "7" || ch == "8" || ch == "9")
      return true;
      return false;
      }
      string decodeString(string s) {
      stack st;
      for(char ch:s) {
      if(ch != ']') {
      st.push(string(1, ch));
      continue;
      }
      string temp = "";
      while(!st.empty() && st.top() != "[") {
      temp = st.top() + temp;
      st.pop();
      }
      if(!st.empty()) {
      st.pop();
      }
      string num = "";
      while(!st.empty() && isDigit(st.top())) {
      num = st.top() + num;
      st.pop();
      }
      string add = "";
      int times = stoi(num);
      while(times--) {
      add += temp;
      }
      st.push(add);
      }
      string result = "";
      while(!st.empty()) {
      result = st.top() + result;
      st.pop();
      }
      return result;
      }

  • @harsh.jain22
    @harsh.jain22 Год назад

    we are using nested while loops so the time complexity is O(n^2) right ?

  • @THIRUORUVAMMEY
    @THIRUORUVAMMEY Месяц назад

    can we use hashmap for this problem sir .. i have solved it using hashmap and the code is like this
    import java.util.HashMap;
    import java.util.Map;
    public class first {
    public static void main(String[] args) {
    String str = "aabbcc";
    char arr[] = str.toCharArray();
    HashMap hm = new HashMap();
    for(int i=0;i

  • @sayantanpoddar5428
    @sayantanpoddar5428 Год назад

    please came up with " Remove Outermost Parentheses" solution

  • @somyaagarwal627
    @somyaagarwal627 7 месяцев назад

    got to know you from instagram
    amazing explaination
    keep stepping up your marketing game !!

  • @letsexplore2093
    @letsexplore2093 11 месяцев назад

    Problem explanation was fabulous but too much while loop to solve this problem that is not optimize I think

  • @amantripathi3501
    @amantripathi3501 Год назад

    Hey,
    Can you help me understand why is this code wrong? I can't seem to figure it out. 😥
    class Solution:
    def compress(self, nums: List[str]) -> int:
    mydict = dict()
    res = ""
    for i in nums:
    if i in mydict.keys():
    mydict[i] += 1
    else:
    mydict[i] = 1
    nums = []
    for key, value in mydict.items():
    nums.append(str(key))
    if value > 1:
    nums.append(str(value))
    print(nums)
    return len(nums)
    PS - I suggest running this code in LeetCode UI for better understanding and also note that I have updated the input list name from 'chars' to 'nums' in the above code.

    • @S3aw0lf
      @S3aw0lf Год назад

      Print and return in same code.

  • @PakIslam2012
    @PakIslam2012 Год назад

    Will your solution work out, if we had an array of all single characters like [a,b,c,d] ?? As we will end up over writing never seen characters

    • @souravjoshi2293
      @souravjoshi2293 Год назад

      It will overwrite the same index characters with the same character
      The output will be {a, b, c, d}. This solution worked for this case as well.

  • @gaurangshroff
    @gaurangshroff Год назад

    what will be the time complexity?

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      It’s linear only. We are iterating over the input array of length n. Each letter visited only once.

  • @daniyaniazics
    @daniyaniazics 2 года назад +1

    in example 3 why its ab12 instead of a1b12

    • @codestorywithMIK
      @codestorywithMIK  2 года назад +1

      Because ‘a’ comes only once and it’s not required to write 1.
      If a letter comes more than once then only we write number after it as per the Qn. You can see example 2 also.

  • @sayanpradhan1366
    @sayanpradhan1366 4 месяца назад

    Can anyone tell me how to think this type of approach ..Currently I'm in my 2nd year but still can't able to think this type of approach.

    • @harsha4048
      @harsha4048 2 месяца назад

      You have more time so don't worry just solve Leetcode problems in a topic wise manner like just pick one sde sheet and take some time and practice more. Everything is gona worth it 🎉

  • @pritishpattnaik4674
    @pritishpattnaik4674 Год назад

    Sir can u please review my code and tell me why am I getting heap buffer overflow error , but 1 example testcase is passing
    int compress(vector& chars) {
    int n = chars.size();
    int ctr = 1;
    char temp = chars[0];
    int next_pos = 1;
    int flag = 0;

    int i;
    for (i=1 ; i= n){
    flag = 1;
    break;
    }
    if (temp != chars[next_pos]){
    continue;
    }
    if (chars[i] == temp){
    ctr++;
    }
    else {
    temp = chars[i];
    chars[next_pos] = ctr + '0';
    ctr = 1;
    int l = next_pos + 1;
    int r = i-1;
    int len = r-l+1;
    chars.erase(chars.begin()+l, chars.begin()+l+len);
    next_pos = i+1;
    }
    }
    chars[next_pos] = ctr + '0';
    chars.erase(chars.begin()+next_pos+1, chars.end());

    for (auto x : chars){
    cout

    • @msingh017
      @msingh017 Год назад

      The heap buffer overflow error in your code is likely due to the use of the chars.erase function inside the loop.

    • @msingh017
      @msingh017 Год назад

      Run This :
      int compress(vector& chars) {
      int n = chars.size();
      int ctr = 1;
      char temp = chars[0];
      int next_pos = 1;
      int flag = 0;
      for (int i = 1; i < n; i++) {
      if (next_pos >= n) {
      flag = 1;
      break;
      }
      if (temp != chars[next_pos]) {
      if (ctr > 1) {
      string ctrStr = to_string(ctr);
      for (char c : ctrStr) {
      chars[next_pos] = c;
      next_pos++;
      }
      }
      temp = chars[i];
      next_pos++;
      ctr = 1;
      } else if (chars[i] == temp) {
      ctr++;
      }
      }
      if (ctr > 1) {
      string ctrStr = to_string(ctr);
      for (char c : ctrStr) {
      chars[next_pos] = c;
      next_pos++;
      }
      }
      chars.erase(chars.begin() + next_pos, chars.end());
      return (flag == 1) ? 1 : chars.size();
      }

  • @isha25tripathi12
    @isha25tripathi12 7 месяцев назад

    Can anyone point the mistake in below code
    var compress = function(chars) {
    let j=1;
    for(let i=0;i