|
| 1 | +--- |
| 2 | +id: word-frequency |
| 3 | +title: Word Frequency |
| 4 | +sidebar_label: 0192 Word Frequency |
| 5 | +tags: |
| 6 | + - Leetcode |
| 7 | + - Bash |
| 8 | + |
| 9 | +description: "This is a solution to the Word Frequency problem." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Write a bash script to calculate the frequency of each word in a text file words.txt. |
| 15 | + |
| 16 | +For simplicity sake, you may assume: |
| 17 | + |
| 18 | +words.txt contains only lowercase characters and space ' ' characters. |
| 19 | +Each word must consist of lowercase characters only. |
| 20 | +Words are separated by one or more whitespace characters. |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | + |
| 26 | +``` |
| 27 | +Assume that words.txt has the following content: |
| 28 | +
|
| 29 | +the day is sunny the the |
| 30 | +the sunny is is |
| 31 | +
|
| 32 | +Your script should output the following, sorted by descending frequency: |
| 33 | +
|
| 34 | +the 4 |
| 35 | +is 3 |
| 36 | +sunny 2 |
| 37 | +day 1 |
| 38 | +
|
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- The input text file `words.txt` contains only lowercase characters and spaces. |
| 45 | +- Words are separated by one or more whitespace characters. |
| 46 | + |
| 47 | +## Solution for Word Frequency Problem |
| 48 | + |
| 49 | +### Intuition And Approach |
| 50 | + |
| 51 | +To solve this problem using Unix tools, we can leverage a series of commands piped together to process the text file. The approach includes: |
| 52 | + |
| 53 | +1. Replacing spaces with newlines to handle word separation. |
| 54 | +2. Sorting the words to prepare for counting duplicates. |
| 55 | +3. Using `uniq` to count the occurrences of each word. |
| 56 | +4. Sorting the counts in descending order. |
| 57 | +5. Formatting the output to display word frequency. |
| 58 | + |
| 59 | +#### Code |
| 60 | + |
| 61 | +<Tabs> |
| 62 | + <TabItem value="bash" label="bash"> |
| 63 | + <SolutionAuthor name="@mahek0620"/> |
| 64 | + ```bash |
| 65 | +tr -s ' ' '\n' < words.txt | sort | uniq -c | sort -nr | awk '{print $2, $1}' |
| 66 | + ``` |
| 67 | + </TabItem> |
| 68 | +</Tabs> |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +## References |
| 73 | + |
| 74 | +- **LeetCode Problem:** [Word frequency Problem](https://leetcode.com/problems/word-frequency/) |
| 75 | +- **Solution Link:** [Word-Frequency Solution on LeetCode](https://leetcode.com/problems/word-frequency/solutions/5273312/word-frequency-solution) |
| 76 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments