LeetCode 192. Word Frequency
分析
写bash,不太会啊……
难度 中
来源
https://leetcode.com/problems/word-frequency/
题目
Write a bash script to calculate the frequency of each word in a text file words.txt
.
For simplicity sake, you may assume:
words.txt
contains only lowercase characters and space' '
characters.- Each word must consist of lowercase characters only.
- Words are separated by one or more whitespace characters.
Example:
Assume that words.txt
has the following content:
the day is sunny the the
the sunny is is
Your script should output the following, sorted by descending frequency:
the 4
is 3
sunny 2
day 1
Note:
- Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.
- Could you write it in one-line using Unix pipes?
解答
https://leetcode.com/problems/word-frequency/discuss/55443/My-simple-solution-(one-line-with-pipe)
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{ print $2, $1 }'
tr -s: truncate the string with target string, but only remaining one instance (e.g. multiple whitespaces)
sort: To make the same string successive so that uniq
could count the same string fully and correctly.
uniq -c: uniq is used to filter out the repeated lines which are successive, -c means counting
sort -r: -r means sorting in descending order
awk '{ print $2, $1 }': To format the output, see here.
LeetCode 192. Word Frequency的更多相关文章
- LeetCode(192. Word Frequency)
192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...
- [leetcode shell]192. Word Frequency
统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...
- 192 Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- Word Frequency
https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of each ...
- [Bash]LeetCode192. 统计词频 | Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Individual Project - Word frequency program-11061171-MaoYu
BUAA Advanced Software Engineering Project: Individual Project - Word frequency program Ryan Mao (毛 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
随机推荐
- Python的网络编程 Socket编程
Socket是进程间通信的一种方式,与其他进程间通信的一个主要不同是:能实现不同主机间的进程间通信,网络上各种各样的服务大多都是基于Socket来完成通信的,要解决网络上两台主机间的通信问题,首先要唯 ...
- 1050. [HAOI2006]旅行【并查集+枚举】
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最 ...
- Day10 API
String类 String是不可变类:值一旦确定了,就不会更改. public static void main(String[] args) { String s1 = "hello&q ...
- Selenium启动最新的火狐浏览器异常排查
报错如下: WebDriverException: Message: 'Can\'t load the profile 打开谷歌浏览器和IE浏览器均正常 网上查阅资料,疑似与selenium版本相关联 ...
- C语言程序设计I—第十一周教学
第十一周教学总结(12/11-17/11) 教学内容 第4章 循环结构-break continue嵌套循环 4.3 判断素数,4.4求1! + 2! + -. + 100! 课前准备 在蓝墨云班课发 ...
- mysql sqlite3 postgresql 简明操作
安装 mysql $ sudo apt-get install mysql-server sqlite3 $ sudo apt-get install sqlite3 postgresql $ sud ...
- oracle快速学习
- CANVAS实现调色板 之 我的第一个随笔
主题代码 <canvas id="color"></canvas> <script> var color=document.getElement ...
- Nivo Slider用法
示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 20155327 实验三 敏捷开发与XP实践
20155327 实验三 敏捷开发与XP实践 实验内容 任务一 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装al ...