{"id":1722,"date":"2018-02-02T08:25:34","date_gmt":"2018-02-02T16:25:34","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1722"},"modified":"2018-08-30T14:00:38","modified_gmt":"2018-08-30T21:00:38","slug":"leetcode-771-jewels-and-stones","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-771-jewels-and-stones\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 771. Jewels and Stones"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u5b9d\u77f3\u7684\u7c7b\u578b\uff0c\u518d\u7ed9\u4f60\u4e00\u5806\u77f3\u5934\uff0c\u8fd4\u56de\u91cc\u9762\u5b9d\u77f3\u7684\u6570\u91cf\u3002<\/p>\n<h1><strong>Problem:<\/strong><\/h1>\n<p>You&#8217;re given strings\u00a0<code>J<\/code>\u00a0representing the types of stones that are jewels, and\u00a0<code>S<\/code>\u00a0representing the stones you have.\u00a0 Each character in\u00a0<code>S<\/code>\u00a0is a type of stone you have.\u00a0 You want to know how many of the stones you have are also jewels.<\/p>\n<p>The letters in\u00a0<code>J<\/code>\u00a0are guaranteed distinct, and all characters in\u00a0<code>J<\/code>\u00a0and\u00a0<code>S<\/code>\u00a0are letters. Letters are case sensitive, so\u00a0<code>\"a\"<\/code>\u00a0is considered a different type of stone from\u00a0<code>\"A\"<\/code>.<\/p>\n<h2><strong>Example 1:<\/strong><\/h2>\n<pre class=\"crayon:false\">Input: J = \"aA\", S = \"aAAbbbb\"\r\nOutput: 3\r\n<\/pre>\n<h2><strong>Example 2:<\/strong><\/h2>\n<pre class=\"crayon:false \">Input: J = \"z\", S = \"ZZ\"\r\nOutput: 0\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>S<\/code>\u00a0and\u00a0<code>J<\/code>\u00a0will consist of letters and have length at most 50.<\/li>\n<li>The characters in\u00a0<code>J<\/code>\u00a0are distinct.<\/li>\n<\/ul>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution 1: HashTable<\/strong><\/h1>\n<p>Time complexity: O(|J| + |S|)<\/p>\n<p>Space complexity: O(128) \/ O(|J|)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  int numJewelsInStones(string J, string S) {\r\n    std::vector&lt;int&gt; f(128, 0);\r\n    int ans = 0;\r\n    for (const char j : J)\r\n      f[j] = 1;\r\n    for (const char s : S)\r\n      ans += f[s] &amp; 1;\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ v2<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 10 ms\r\nclass Solution {\r\npublic:\r\n  int numJewelsInStones(const string&amp; J, const string&amp; S) {\r\n    std::set&lt;char&gt; f(J.begin(), J.end());\r\n    return std::count_if(S.begin(), S.end(), \r\n                         [&amp;f](const char c) { return f.count(c); });\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 18 ms\r\nclass Solution {\r\n  public int numJewelsInStones(String J, String S) {\r\n    int[] f = new int[128];\r\n    for (final char c : J.toCharArray())\r\n      f[c] = 1;\r\n    int ans = 0;\r\n    for (final char c : S.toCharArray())\r\n      ans += f[c];\r\n    return ans;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 43 ms\r\n\"\"\"\r\nclass Solution(object):\r\n  def numJewelsInStones(self, J, S):\r\n    f = set(J)\r\n    return sum([s in f for s in S])<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u5b9d\u77f3\u7684\u7c7b\u578b\uff0c\u518d\u7ed9\u4f60\u4e00\u5806\u77f3\u5934\uff0c\u8fd4\u56de\u91cc\u9762\u5b9d\u77f3\u7684\u6570\u91cf\u3002 Problem: You&#8217;re given strings\u00a0J\u00a0representing the types of stones that are jewels, and\u00a0S\u00a0representing the stones you have.\u00a0 Each character in\u00a0S\u00a0is a type of stone you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[254,222,82,4],"class_list":["post-1722","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-counter","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1722","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=1722"}],"version-history":[{"count":12,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1722\/revisions"}],"predecessor-version":[{"id":3779,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1722\/revisions\/3779"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}