{"id":7718,"date":"2020-11-28T10:22:33","date_gmt":"2020-11-28T18:22:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7718"},"modified":"2020-11-28T10:53:38","modified_gmt":"2020-11-28T18:53:38","slug":"leetcode-1668-maximum-repeating-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1668-maximum-repeating-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1668. Maximum Repeating Substring"},"content":{"rendered":"\n<p>For a string&nbsp;<code>sequence<\/code>, a string&nbsp;<code>word<\/code>&nbsp;is&nbsp;<strong><code>k<\/code>-repeating<\/strong>&nbsp;if&nbsp;<code>word<\/code>&nbsp;concatenated&nbsp;<code>k<\/code>&nbsp;times is a substring of&nbsp;<code>sequence<\/code>. The&nbsp;<code>word<\/code>&#8216;s&nbsp;<strong>maximum&nbsp;<code>k<\/code>-repeating value<\/strong>&nbsp;is the highest value&nbsp;<code>k<\/code>&nbsp;where&nbsp;<code>word<\/code>&nbsp;is&nbsp;<code>k<\/code>-repeating in&nbsp;<code>sequence<\/code>. If&nbsp;<code>word<\/code>&nbsp;is not a substring of&nbsp;<code>sequence<\/code>,&nbsp;<code>word<\/code>&#8216;s maximum&nbsp;<code>k<\/code>-repeating value is&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p>Given strings&nbsp;<code>sequence<\/code>&nbsp;and&nbsp;<code>word<\/code>, return&nbsp;<em>the&nbsp;<strong>maximum&nbsp;<code>k<\/code>-repeating value<\/strong>&nbsp;of&nbsp;<code>word<\/code>&nbsp;in&nbsp;<code>sequence<\/code><\/em>.<\/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> sequence = \"ababc\", word = \"ab\"\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>\"abab\" is a substring in \"ababc\".\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> sequence = \"ababc\", word = \"ba\"\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>\"ba\" is a substring in \"ababc\". \"baba\" is not a substring in \"ababc\".\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> sequence = \"ababc\", word = \"ac\"\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>\"ac\" is not a substring in \"ababc\". \n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= sequence.length &lt;= 100<\/code><\/li><li><code>1 &lt;= word.length &lt;= 100<\/code><\/li><li><code>sequence<\/code>&nbsp;and&nbsp;<code>word<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<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++\">class Solution {\npublic:\n  int maxRepeating(string sequence, string word) {\n    string s(word);\n    for (int i = 1;;++i) {\n      if (sequence.find(s) == string::npos) return i - 1;\n      s += word;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For a string&nbsp;sequence, a string&nbsp;word&nbsp;is&nbsp;k-repeating&nbsp;if&nbsp;word&nbsp;concatenated&nbsp;k&nbsp;times is a substring of&nbsp;sequence. The&nbsp;word&#8216;s&nbsp;maximum&nbsp;k-repeating value&nbsp;is the highest value&nbsp;k&nbsp;where&nbsp;word&nbsp;is&nbsp;k-repeating in&nbsp;sequence. If&nbsp;word&nbsp;is not a substring of&nbsp;sequence,&nbsp;word&#8216;s maximum&nbsp;k-repeating value is&nbsp;0. Given strings&nbsp;sequence&nbsp;and&nbsp;word,&#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":[222,4,314],"class_list":["post-7718","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7718","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=7718"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7718\/revisions"}],"predecessor-version":[{"id":7720,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7718\/revisions\/7720"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}