LeetCode 192: Word Frequency
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:
cat words.txt
: Display the contents of the text file.tr ' ' '\n'
: Replace spaces with newlines to split the words.sed '/^$/d'
: Remove empty lines.sort
: Sort the words.uniq -c
: Count the frequency of each unique word.sort -nr
: Sort based on frequency in descending order.awk '{print $2, $1}'
: Print the word and its frequency.
Solutions
// Java solution not applicable as the problem is specifically for bash scripting.
Related LeetCode Problems
Loading editor...