Nav: << previous: 191.位1的个数 | next: 193.有效电话号码 >>
Description
tab: English
<p>Write a bash script to calculate the <span data-keyword="frequency-textfile">frequency</span> of each word in a text file <code>words.txt</code>.</p>
<p>For simplicity sake, you may assume:</p>
<ul>
<li><code>words.txt</code> contains only lowercase characters and space <code>' '</code> characters.</li>
<li>Each word must consist of lowercase characters only.</li>
<li>Words are separated by one or more whitespace characters.</li>
</ul>
<p><strong class="example">Example:</strong></p>
<p>Assume that <code>words.txt</code> has the following content:</p>
<pre>
the day is sunny the the
the sunny is is
</pre>
<p>Your script should output the following, sorted by descending frequency:</p>
<pre>
the 4
is 3
sunny 2
day 1
</pre>
<p><b>Note:</b></p>
<ul>
<li>Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.</li>
<li>Could you write it in one-line using <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html">Unix pipes</a>?</li>
</ul>
---
[submissions](https://leetcode.com/problems/word-frequency/submissions/) | [solutions](https://leetcode.com/problems/word-frequency/solutions/)
tab: 中文
<p>写一个 bash 脚本以统计一个文本文件 <code>words.txt</code> 中每个单词出现的<span data-keyword="frequency-textfile">频率</span>。</p>
<p>为了简单起见,你可以假设:</p>
<ul>
<li><code>words.txt</code>只包括小写字母和 <code>' '</code> 。</li>
<li>每个单词只由小写字母组成。</li>
<li>单词间由一个或多个空格字符分隔。</li>
</ul>
<p><strong>示例:</strong></p>
<p>假设 <code>words.txt</code> 内容如下:</p>
<pre>
the day is sunny the the
the sunny is is
</pre>
<p>你的脚本应当输出(以词频降序排列):</p>
<pre>
the 4
is 3
sunny 2
day 1
</pre>
<p><strong>说明:</strong></p>
<ul>
<li>不要担心词频相同的单词的排序问题,每个单词出现的频率都是唯一的。</li>
<li>你可以使用一行 <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html">Unix pipes</a> 实现吗?</li>
</ul>
---
[提交记录](https://leetcode.cn/problems/word-frequency/submissions/) | [题解](https://leetcode.cn/problems/word-frequency/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122