{"id":8281,"date":"2021-03-27T21:34:53","date_gmt":"2021-03-28T04:34:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8281"},"modified":"2021-03-27T21:35:43","modified_gmt":"2021-03-28T04:35:43","slug":"leetcode-1805-number-of-different-integers-in-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1805-number-of-different-integers-in-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1805. Number of Different Integers in a String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>word<\/code>&nbsp;that consists of digits and lowercase English letters.<\/p>\n\n\n\n<p>You will replace every non-digit character with a space. For example,&nbsp;<code>\"a123bc34d8ef34\"<\/code>&nbsp;will become&nbsp;<code>\" 123&nbsp; 34 8&nbsp; 34\"<\/code>. Notice that you are left with some integers that are separated by at least one space:&nbsp;<code>\"123\"<\/code>,&nbsp;<code>\"34\"<\/code>,&nbsp;<code>\"8\"<\/code>, and&nbsp;<code>\"34\"<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of&nbsp;<strong>different<\/strong>&nbsp;integers after performing the replacement operations on&nbsp;<\/em><code>word<\/code>.<\/p>\n\n\n\n<p>Two integers are considered different if their decimal representations&nbsp;<strong>without any leading zeros<\/strong>&nbsp;are different.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"a123bc34d8ef34\"\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The three different integers are \"123\", \"34\", and \"8\". Notice that \"34\" is only counted once.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"leet1234code234\"\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"a1b01c001\"\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>The three integers \"1\", \"01\", and \"001\" all represent the same integer because\nthe leading zeros are ignored when comparing their decimal values.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word.length &lt;= 1000<\/code><\/li><li><code>word<\/code>&nbsp;consists of digits and lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Be careful about leading zeros.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numDifferentIntegers(string word) {\n    word += \"$\";\n    unordered_set<string> s;\n    deque<char> cur;\n    for (char c : word) {\n      if (isdigit(c)) {       \n        cur.push_back(c);\n      } else if (!cur.empty()) {      \n        while (cur.size() > 1 && cur[0] == '0')\n          cur.pop_front();\n        s.insert({begin(cur), end(cur)});\n        cur.clear();\n      }\n    }\n    return s.size();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;word&nbsp;that consists of digits and lowercase English letters. You will replace every non-digit character with a space. For example,&nbsp;&#8220;a123bc34d8ef34&#8243;&nbsp;will become&nbsp;&#8221; 123&nbsp;&#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],"tags":[222,82,4],"class_list":["post-8281","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8281","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=8281"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8281\/revisions"}],"predecessor-version":[{"id":8283,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8281\/revisions\/8283"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}