{"id":12219,"date":"2026-04-25T16:57:00","date_gmt":"2026-04-25T11:27:00","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=12219"},"modified":"2026-05-10T15:40:52","modified_gmt":"2026-05-10T10:10:52","slug":"repeated-string-match","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/repeated-string-match\/","title":{"rendered":"Repeated String Match"},"content":{"rendered":"\n\n<link\n    href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\"\n    rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_theme {\n  --primary: #E58C32;\n  --secondary: #030302;\n  --light-bg: #fef9f4;\n  --text-dark: #2d2d2d;\n  --tab-radius: 12px;\n  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n  --code-bg: #001f3f;\n  --code-text: #d4f1ff;\n}\n\n.wp_blog_container {\n  font-family: 'Segoe UI', sans-serif;\n  background: var(--light-bg);\n  margin: 0;\n  padding: 0;\n  color: var(--text-dark);\n}\n\n\/* Heading *\/\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: var(--primary);\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n\/* Explanation Card *\/\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  background: white;\n  border-radius: var(--tab-radius);\n  box-shadow: var(--shadow);\n}\n\n\/* Text and Visuals *\/\n.wp_blog_explanation h2 {\n  font-size: 1.4rem;\n  color: var(--primary);\n  margin-bottom: 0.5rem;\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n}\n\n.wp_blog_explanation code {\n  background: #fef9f4;   \/* light bg instead of dark blue *\/\n  color: #E58C32;        \/* brand orange *\/\n  padding: 3px 6px;\n  border-radius: 4px;\n  font-family: 'Courier New', monospace;\n  font-weight: 600;      \/* optional, makes it pop *\/\n}\n\n.wp_blog_explanation img {\n  max-width: 100%;\n  border-radius: var(--tab-radius);\n  margin-top: 1rem;\n  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);\n}\n\n\/* Tab Buttons *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid var(--primary);\n  background: white;\n  color: var(--primary);\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  transition: all 0.3s ease;\n}\n\n.wp_blog_code-tab-button:hover {\n  background: var(--secondary);\n}\n\n.wp_blog_code-tab-button.active {\n  background: var(--primary);\n  color: white;\n}\n\n\/* Code Content *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n}\n\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n  color: var(--code-text);\n}\n\n\/* Dark mode variables *\/\n.wp_blog_theme.dark-mode {\n  --light-bg: #121212;\n  --text-dark: #f5f5f5;\n  --shadow: 0 4px 12px rgba(255, 255, 255, 0.08);\n  --code-bg: #1e1e1e;\n  --code-text: #c5f0ff;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_explanation {\n  background: #1e1e1e;\n}\n\n\/* Dark mode code highlight *\/\n.wp_blog_theme.dark-mode .wp_blog_explanation code {\n  background: #333;\n  color: #ffd27f;\n}\n\n.wp_blog_theme {\n  position: relative; \/* makes it the reference for absolute children *\/\n}\n\n.wp_blog_toggle-btn {\n  position: absolute;\n  top: 1rem;\n  right: 1rem;\n  z-index: 9999;\n  padding: 0.5rem 0.8rem;\n  border-radius: 10%;\n  background: var(--primary);\n  color: white;\n  font-weight: bold;\n  cursor: pointer;\n  border: none;\n  box-shadow: var(--shadow);\n  transition: background 0.3s, transform 0.2s;\n}\n\n.wp_blog_toggle-btn:hover {\n  background: #cc772e;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_code-tabs-container {\n  background: #1e1e1e;\n}\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\"> \n      <button id=\"blogNotesThemeToggle\" class=\"wp_blog_toggle-btn\">\ud83c\udf19<\/button>\n    <h1 class=\"wp_blog_main-heading\"><\/h1>\n    <div class=\"wp_blog_explanation\">\n        <h2>Problem Statement:<\/h2>\n        <p>\n            Given two strings <code>a<\/code> and <code>b<\/code>, return <i>the minimum number of times you should repeat string<\/i> <code>a<\/code> <i>so that string<\/i> <code>b<\/code> is a <i>substring of it<\/i>. If it is impossible for <code>b<\/code> to be a substring of <code>a<\/code> after repeating it, return <code>-1<\/code>.\n        <\/p>\n\n        <p><strong>Notice<\/strong>: string <code>\"abc\"<\/code> repeated 0 times is <code>\"\"<\/code>, repeated 1 time is <code>\"abc\"<\/code> and repeated 2 times is <code>\"abcabc\"<\/code>.<\/p>\n\n                <h2>Examples:<\/h2>\n\n                <h3>Example 1:<\/h3>\n                <p><strong>Input:<\/strong> a = &#8220;abcd&#8221;, b = &#8220;cdabcdab&#8221;\n                <\/p>\n                <p><strong>Output:<\/strong> 3<\/p>\n                <p><strong>Explanation:<\/strong> We return 3 because by repeating a three times &#8220;abcdabcdabcd&#8221;, b is a substring of it.<\/p>\n\n                <h3>Example 2:<\/h3>\n                <p><strong>Input:<\/strong>  a = &#8220;a&#8221;, b = &#8220;aa&#8221;<\/p>\n\n                <p><strong>Output:<\/strong> 2<\/p>\n\n                <!-- <h3>Example 3:<\/h3>\n                <p><strong>Input:<\/strong> s = \"2[abc]3[cd]ef\"<\/p>\n\n                <p><strong>Output:<\/strong> \"abcabccdcdcdef\"<\/p> \n\n                <h5>Example 3:<\/h5>\n                <p><strong>Input:<\/strong>\n                <\/p>\n                <p>\n                    nums1 = [0], m = 0\n                    nums2 = [1], n = 1\n                <\/p>    \n                <p><strong>Output:<\/strong>[1]<\/p>\n                  <p>\n                  <code>Explanation:<\/code>\n                 The arrays we are merging are [] and [1].\n                    The result of the merge is [1].\n                    Note that because m = 0, there are no elements in nums1. \n                    The 0 is only there to ensure the merge result can fit in nums1.\n                <\/p> -->\n\n                <h2>Constraints<\/h2>\n                <p><code>1 <= a.length, b.length <= 10<sup>4<\/sup><\/code><\/p>\n                <p><code>a<\/code> and <code>b<\/code> consist of lowercase English letters.<\/p>\n\n                    <h2>Time Complexity:<\/h2>\n                    <li>\n                      <p><strong>Time Complexity = O(m * (m \/ n)) \u2248 O(m\u00b2 \/ n)<\/strong>\n                      <\/li>\n                    <h2>Space Complexity:<\/h2>\n                    <li>\n                      <p><strong>Space Complexity = O(m)<\/strong><\/p>\n                    <\/li>\n\n<h2>Approach<\/h2>\n<ul>\n    <li>Start with <code>text = a <\/code>and <code>count = 1<\/code> (first repetition).<\/li>\n    <li>Since <strong>b can only appear when text is at least as long as b<\/strong>, keep appending a to text and <code>increment count until text.length \u2265 b.length<\/code>.<\/li>\n    <li>Once the <strong>length condition is met<\/strong>, check if <code>b exists inside text:<\/code>\n      <ul>\n        <li>If <strong>yes \u2192 return count<\/strong>.<\/li>\n        <li>If <code>not found<\/code>, <strong>append a one more time <\/strong>(this handles cases where b spans across boundaries of repeated a).<\/li>\n      <\/ul>\n    <\/li>\n    <li><strong>Check again for substring:<\/strong> \n      <ul>\n        <li><code>If found \u2192 return updated count<\/code>.<\/li>\n        <li>If <strong>b is still not found<\/strong>, it means <code>it can never be formed \u2192 return -1<\/code>.<\/li>\n      <\/ul>\n    <\/li>\n\n    <li><strong>Minimum repeats<\/strong> = to match <code>length of b<\/code><\/li>\n  <li><code>Maximum needed = that + 1 <\/code>(for overlap cases) <\/li>\n<\/ul>\n\n\n<h2>Dry Run<\/h2>\n<div style=\"background: var(--light-bg); border-left: 4px solid var(--primary); padding: 1rem; border-radius: var(--tab-radius); margin: 1rem 0; color: var(--text-dark);\"> \n  <p><strong>Input:<\/strong> <code>a = \"abcd\", b = \"cdabcdab\"<\/code><\/p> \n  \n  <pre style=\"white-space: pre-wrap; background: var(--code-bg); padding: 1rem; border-radius: 8px; overflow-x: auto; color: var(--code-text);\">\nInitial:\n   count = 1\n   text = \"abcd\"\n\nWhile loop (text.length < b.length):\n\n   Iteration 1:\n      text.length = 4 < 8\n      text = \"abcd\" + \"abcd\" = \"abcdabcd\"\n      count = 2\n\nNow:\n   text.length = 8 (not < 8) \u2192 exit loop\n\nCheck:\n   \"abcdabcd\".includes(\"cdabcdab\") \u2192 false\n\nAdd one more repetition (edge case):\n   text = \"abcdabcd\" + \"abcd\" = \"abcdabcdabcd\"\n   count = 3\n\nCheck again:\n   \"abcdabcdabcd\".includes(\"cdabcdab\") \u2192 true\n\nFinal:\n   return 3\n  <\/pre> \n  \n  <p><strong>Output:<\/strong> <code>3<\/code><\/p> \n<\/div>\n<\/div>\n\n\n<div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n        <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">\n            JavaScript\n        <\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n        <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n        <pre><code class=\"language-javascript\">\nvar repeatedStringMatch = function(a, b) {\n    let count = 1;\n    let text = a;\n\n    \/\/ Keep appending until length >= b\n    while (text.length < b.length) {\n        text += a;\n        count++;\n    }\n\n    \/\/ Check if b is now a substring\n    if (text.includes(b)) return count;\n\n    \/\/ One extra append (edge case)\n    text += a;\n    count++;\n\n    if (text.includes(b)) return count;\n\n    return -1;\n};\n    <\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n        <pre><code class=\"language-python\">\ndef repeatedStringMatch(a, b):\n    count = 1\n    text = a\n\n    while len(text) < len(b):\n        text += a\n        count += 1\n\n    if b in text:\n        return count\n\n    text += a\n    count += 1\n\n    if b in text:\n        return count\n\n    return -1\n    <\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n        <pre><code class=\"language-java\">\nclass Solution {\n    public int repeatedStringMatch(String a, String b) {\n        int count = 1;\n        StringBuilder text = new StringBuilder(a);\n\n        while (text.length() < b.length()) {\n            text.append(a);\n            count++;\n        }\n\n        if (text.toString().contains(b)) return count;\n\n        text.append(a);\n        count++;\n\n        if (text.toString().contains(b)) return count;\n\n        return -1;\n    }\n}\n<\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n        <pre><code class=\"language-cpp\">\nclass Solution {\npublic:\n    int repeatedStringMatch(string a, string b) {\n        int count = 1;\n        string text = a;\n\n        while (text.length() < b.length()) {\n            text += a;\n            count++;\n        }\n\n        if (text.find(b) != string::npos) return count;\n\n        text += a;\n        count++;\n\n        if (text.find(b) != string::npos) return count;\n\n        return -1;\n    }\n};\n    <\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n        <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint repeatedStringMatch(char* a, char* b) {\n    static char text[10000];  \/\/ assume enough size\n    strcpy(text, a);\n    int count = 1;\n\n    while (strlen(text) < strlen(b)) {\n        strcat(text, a);\n        count++;\n    }\n\n    if (strstr(text, b) != NULL) return count;\n\n    strcat(text, a);\n    count++;\n\n    if (strstr(text, b) != NULL) return count;\n\n    return -1;\n}\n   <\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n        <pre><code class=\"language-csharp\">\n\npublic class Solution {\n    public int RepeatedStringMatch(string a, string b) {\n        int count = 1;\n        string text = a;\n\n        while (text.Length < b.Length) {\n            text += a;\n            count++;\n        }\n\n        if (text.Contains(b)) return count;\n\n        text += a;\n        count++;\n\n        if (text.Contains(b)) return count;\n\n        return -1;\n    }\n}\n\n  <\/code><\/pre>\n    <\/div>\n<\/div>\n<\/div>\n\n<script>\ndocument.addEventListener(\"DOMContentLoaded\", () => {\n  const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n  const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n  buttons.forEach((button) => {\n    button.addEventListener(\"click\", () => {\n      const lang = button.getAttribute(\"data-lang\");\n\n      buttons.forEach((btn) => btn.classList.remove(\"active\"));\n      contents.forEach((content) => content.classList.remove(\"active\"));\n\n      button.classList.add(\"active\");\n      document\n        .querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`)\n        .classList.add(\"active\");\n    });\n  });\n\n  const themeToggle = document.getElementById(\"blogNotesThemeToggle\");\n  const themeContainer = document.querySelector(\".wp_blog_theme\");\n\n  themeToggle.addEventListener(\"click\", () => {\n    themeContainer.classList.toggle(\"dark-mode\");\n    themeToggle.textContent =\n      themeContainer.classList.contains(\"dark-mode\") ? \"\u2600\ufe0f\" : \"\ud83c\udf19\";\n  });\n});\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udf19 Problem Statement: Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b to be a substring of a after repeating it, return -1. Notice: string &#8220;abc&#8221; repeated 0 times is &#8220;&#8221;, repeated<\/p>\n","protected":false},"author":108,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[258,322,260,176,175,211,811,810,174,172,1],"tags":[],"class_list":["post-12219","post","type-post","status-publish","format-standard","category-advance-java","category-algorithms-and-data-structures","category-c-c-plus-plus","category-csharp","category-cplusplus","category-data-structures","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/12219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=12219"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/12219\/revisions"}],"predecessor-version":[{"id":12220,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/12219\/revisions\/12220"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=12219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=12219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=12219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}