{"id":6065,"date":"2020-01-11T09:34:57","date_gmt":"2020-01-11T17:34:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6065"},"modified":"2020-01-11T09:35:35","modified_gmt":"2020-01-11T17:35:35","slug":"leetcode-1316-distinct-echo-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1316-distinct-echo-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1316. Distinct Echo Substrings"},"content":{"rendered":"\n<p>Return the number of&nbsp;<strong>distinct<\/strong>&nbsp;non-empty substrings of&nbsp;<code>text<\/code>&nbsp;that can be written as the concatenation of some string with itself.<\/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> text = \"abcabcabc\"\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The 3 substrings are \"abcabc\", \"bcabca\" and \"cabcab\".\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> text = \"leetcodeleetcode\"\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>The 2 substrings are \"ee\" and \"leetcodeleetcode\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= text.length &lt;= 2000<\/code><\/li><li><code>text<\/code>&nbsp;has only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1:  Brute Force + HashSet<\/strong><\/h2>\n\n\n\n<p>Try all possible substrings<\/p>\n\n\n\n<p>Time complexity: O(n^3)<br>Space complexity: O(n^2)<\/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 distinctEchoSubstrings(string text) {\n    const int n = text.length();\n    string_view t(text);\n    unordered_set<string_view> s;    \n    for (int k = 1; k <= n \/ 2; ++k)\n      for (int i = 0; i + k <= n; ++i)\n        if (t.substr(i, k) == t.substr(i + k, k))\n          s.insert(t.substr(i, 2 * k));\n    return s.size();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Return the number of&nbsp;distinct&nbsp;non-empty substrings of&nbsp;text&nbsp;that can be written as the concatenation of some string with itself. Example 1: Input: text = &#8220;abcabcabc&#8221; Output: 3&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[217,114,299,4],"class_list":["post-6065","post","type-post","status-publish","format-standard","hentry","category-string","tag-hard","tag-hash","tag-hashset","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6065","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=6065"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6065\/revisions"}],"predecessor-version":[{"id":6067,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6065\/revisions\/6067"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}