Sort Characters by Frequency | LeetCode 451 | C++, Java, Python | May LeetCoding Day 22

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 11

  • @vaibhavmalik6490
    @vaibhavmalik6490 Před 4 lety +6

    We can use maxHeap of pair by first computing freq of each elem in freq [26] array and then putting each pair in maxHeap.

    • @paragroy5359
      @paragroy5359 Před 3 lety

      I think we have to take the hash map bcz the character could be uppercase ,lowercase as well as it could be a digit.

  • @amitmandal5842
    @amitmandal5842 Před 8 měsíci

    Nice explanation..

  • @TheArbaaz-rn2tq
    @TheArbaaz-rn2tq Před 4 lety

    Why we use lambda function in key?

  • @aiyanshahid7374
    @aiyanshahid7374 Před 4 lety +2

    why am i getting tle when my and your code is same

  • @rishidhawan9323
    @rishidhawan9323 Před 4 lety +2

    string frequencySort(string s) {
    int freq[256]= {0};
    string res;
    priority_queue maxHeap;
    for (int i=0; i

  • @Star_Bawa9
    @Star_Bawa9 Před 2 lety

    I have not understood the Collections . sort part

  • @anuragsandhu9590
    @anuragsandhu9590 Před 2 lety +1

    Concept Used: All numeric keys in object in javascript are sorted in ascending order | O(n) time complexity | O(26) space complexity
    var frequencySort = function (s) {
    const obj = {};
    for (let index = 0; index < s.length; index++) {
    if (!obj[s[index]]) {
    obj[s[index]] = 1;
    } else {
    obj[s[index]] += 1;
    }
    }
    const convertedObj = {};
    for (const key in obj) {
    if (convertedObj[obj[key]]) {
    convertedObj[obj[key]][key] = key.repeat(obj[key]);
    } else {
    convertedObj[obj[key]] = { [key]: key.repeat(obj[key]) };
    }
    }
    const keys = Object.keys(convertedObj);
    let str = '';
    for (let index = keys.length - 1; index >= 0; index--) {
    for (const key in convertedObj[keys[index]]) {
    str += convertedObj[keys[index]][key];
    }
    }
    return str;
    };