LeetCode192 solution

Printing out Word Frequency

The original problem is https://leetcode.com/problems/word-frequency/.

In fact, counting word frequency is a typical application of bash. For example, Example 16-12 in this bash guide.

The useful one-liner for word frequency analysis is

cat words.txt | tr ' ' '\n' | sed -E '/^\s*$/d' | sort | uniq -c | sort -nr

We just need to switch the first and second columns, and that's it.

Solution:

cat words.txt | tr ' ' '\n' | sed -E '/^\s*$/d' | sort | uniq -c | sort -nr | awk ' { t = $1; $1 = $2; $2 = t; print; } '