LeetCode 192: Word Frequency

Shell

Problem Description

Explanation

To solve this problem, we can use Unix commands like cat, tr, sed, sort, uniq, and awk in a pipeline to count the frequency of each word in the text file. Here's a step-by-step approach:

  1. cat words.txt: Display the contents of the text file.
  2. tr ' ' '\n': Replace spaces with newlines to split the words.
  3. sed '/^$/d': Remove empty lines.
  4. sort: Sort the words.
  5. uniq -c: Count the frequency of each unique word.
  6. sort -nr: Sort based on frequency in descending order.
  7. awk '{print $2, $1}': Print the word and its frequency.

Solutions

// Java solution not applicable as the problem is specifically for bash scripting.

Loading editor...