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

Поділитися
Вставка
  • Опубліковано 19 вер 2024

КОМЕНТАРІ • 11

  • @vaibhavmalik6490
    @vaibhavmalik6490 4 роки тому +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 3 роки тому

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

    Nice explanation..

  • @TheArbaaz-rn2tq
    @TheArbaaz-rn2tq 4 роки тому

    Why we use lambda function in key?

  • @aiyanshahid7374
    @aiyanshahid7374 4 роки тому +2

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

  • @rishidhawan9323
    @rishidhawan9323 4 роки тому +2

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

  • @Star_Bawa9
    @Star_Bawa9 2 роки тому

    I have not understood the Collections . sort part

  • @anuragsandhu9590
    @anuragsandhu9590 2 роки тому +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;
    };