LeetCode-192: Word Frequency

A comprehensive solution and detailed explanation for solving LeetCode Problem 192: Word Frequency. This guide includes the problem statement, approach, and code implementation.

192. Word Frequency

Solution

1cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2 " " $1}'

Word Frequency Explanation

Here is a breakdown of each command in the pipeline:

CommandExplanation
cat words.txtReads the content of the file words.txt.
tr -s ' ' '\n'Transforms spaces into newlines, putting each word on a separate line.
sortSorts the words alphabetically.
uniq -cCounts the occurrences of each word and prefixes each word with its count.
sort -rSorts the words by frequency in descending order.
awk '{print $2 " " $1}'Formats the output to display the word followed by its frequency.

Example Workflow

Given the input file words.txt containing:

1this is a test this is only a test

The pipeline processes it as follows:

StepCommandOutput
Read file contentcat words.txtthis is a test this is only a test
Transform spaces to newlinestr -s ' ' '\n'this\nis\na\ntest\nthis\nis\nonly\na\ntest\n
Sort words alphabeticallysorta\na\nis\nis\nonly\ntest\ntest\nthis\nthis\n
Count word occurrencesuniq -c2 a\n2 is\n1 only\n2 test\n2 this\n
Sort by frequency descendingsort -r2 this\n2 test\n2 is\n2 a\n1 only\n
Format outputawk '{print $2 " " $1}'this 2\ntest 2\nis 2\na 2\nonly 1\n

Final Output

1this 2
2test 2
3is 2
4a 2
5only 1
comments powered by Disqus