{"id":2394,"date":"2018-03-28T20:58:39","date_gmt":"2018-03-29T03:58:39","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2394"},"modified":"2018-09-03T22:31:41","modified_gmt":"2018-09-04T05:31:41","slug":"leetcode-192-word-frequency","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bash\/leetcode-192-word-frequency\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 192. Word Frequency"},"content":{"rendered":"<p>Write a bash script to calculate the frequency of each word in a text file\u00a0<code>words.txt<\/code>.<\/p>\n<p>For simplicity sake, you may assume:<\/p>\n<ul>\n<li><code>words.txt<\/code>\u00a0contains only lowercase characters and space\u00a0<code>' '<\/code>\u00a0characters.<\/li>\n<li>Each word must consist of lowercase characters only.<\/li>\n<li>Words are separated by one or more whitespace characters.<\/li>\n<\/ul>\n<p>For example, assume that\u00a0<code>words.txt<\/code>\u00a0has the following content:<\/p>\n<pre class=\"crayon:false\">the day is sunny the the\r\nthe sunny is is\r\n<\/pre>\n<p>Your script should output the following, sorted by descending frequency:<\/p>\n<pre class=\"crayon:false\">the 4\r\nis 3\r\nsunny 2\r\nday 1\r\n<\/pre>\n<p><b>Note:<\/b><br \/>\nDon&#8217;t worry about handling ties, it is guaranteed that each word&#8217;s frequency count is unique.<\/p>\n<h1><strong>Solution<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">bash<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:sh decode:true\"># Author: Huahua\r\n# Running time: 8 ms (beats 99.80%)\r\ncat words.txt | tr -s \" \" \"\\n\" | sort | uniq -c | sort -r | awk '{print $2, $1}'<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Write a bash script to calculate the frequency of each word in a text file\u00a0words.txt. For simplicity sake, you may assume: words.txt\u00a0contains only lowercase characters&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[280],"tags":[282,23,283,281],"class_list":["post-2394","post","type-post","status-publish","format-standard","hentry","category-bash","tag-awk","tag-sort","tag-tr","tag-uniq","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2394"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2394\/revisions"}],"predecessor-version":[{"id":3845,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2394\/revisions\/3845"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}