My 1 line solution using awk, sort and pipe


  • 3

    First, use awk to count the number for each word.
    Then sort to sort the result by decreasing order.

    awk '{for(i=1;i<=NF;i++) a[$i]++} END {for(k in a) print k,a[k]}' words.txt | sort -k2 -nr

  • 0
    C

    why there is memory exceed error while running this solution?


  • 0
    S

    This command cannot work for string like "how many cans \n can a canner can if a canner can can cans\na canner can can as many cans as a canner can if a canner can can a can\na canner "


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.