{"id":6523,"date":"2020-03-21T22:37:00","date_gmt":"2020-03-22T05:37:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6523"},"modified":"2020-03-21T22:37:34","modified_gmt":"2020-03-22T05:37:34","slug":"leetcode-1392-longest-happy-prefix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1392-longest-happy-prefix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1392. Longest Happy Prefix"},"content":{"rendered":"\n<p>A string is called a&nbsp;<em>happy prefix<\/em>&nbsp;if is a&nbsp;<strong>non-empty<\/strong>&nbsp;prefix which is also a suffix (excluding itself).<\/p>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>. Return the&nbsp;<strong>longest happy prefix<\/strong>&nbsp;of&nbsp;<code>s<\/code>&nbsp;.<\/p>\n\n\n\n<p>Return an empty string if no such prefix exists.<\/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> s = \"level\"\n<strong>Output:<\/strong> \"l\"\n<strong>Explanation:<\/strong> s contains 4 prefix excluding itself (\"l\", \"le\", \"lev\", \"leve\"), and suffix (\"l\", \"el\", \"vel\", \"evel\"). The largest prefix which is also suffix is given by \"l\".\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> s = \"ababab\"\n<strong>Output:<\/strong> \"abab\"\n<strong>Explanation:<\/strong> \"abab\" is the largest prefix which is also suffix. They can overlap in the original string.\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> s = \"leetcodeleet\"\n<strong>Output:<\/strong> \"leet\"\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"a\"\n<strong>Output:<\/strong> \"\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10^5<\/code><\/li><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Rolling Hash<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n) \/ worst case: O(n^2)<br>Space complexity: O(1)<\/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  string longestPrefix(string_view s) {\n    const int kMod = 16769023;\n    const int kBase = 128;\n    const int n = s.length();\n    int p = 0;\n    int q = 0;\n    int a = 1;\n    int ans = 0;\n    for (int i = 1; i < n; ++i) {\n      p = (p + a * s[i - 1]) % kMod;\n      q = (q * kBase + s[n - i]) % kMod;      \n      a = (a * kBase) % kMod;\n      if (p == q &#038;&#038; s.substr(0, i) == s.substr(n - i))\n        ans = i;\n    }\n    return string(s.substr(0, ans));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A string is called a&nbsp;happy prefix&nbsp;if is a&nbsp;non-empty&nbsp;prefix which is also a suffix (excluding itself). Given a string&nbsp;s. Return the&nbsp;longest happy prefix&nbsp;of&nbsp;s&nbsp;. Return an empty&#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,572,571,4],"class_list":["post-6523","post","type-post","status-publish","format-standard","hentry","category-string","tag-hard","tag-kmp","tag-rolling-hash","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6523","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=6523"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6523\/revisions"}],"predecessor-version":[{"id":6525,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6523\/revisions\/6525"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}