{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/stringMatchingUsingHorspoolsAlgorithm","result":{"data":{"blog":{"frontmatter":{"title":"STRING MATCHING USING HORSPOOL'S ALGORITHM","thumbnail":"blog68","date":"June 1, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1nEoRrO50UYYO-mXWqA5yiX7epayVNr1o/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  In string matching algorithm, we check whether a text T\n                  contains a pattern P(length of T>P) i.e to check whether P is\n                  a substring of T or not. Since we are trying to check a fixed\n                  string P, sometimes these algorithms are called exact string\n                  matching algorithms.\n                </p>\n                <p>\n                  In horspool's method, we shift the pattern for matching by\n                  looking at the character(say c) of the text that is aligned\n                  against the last character of the pattern. This is the case\n                  even if character c itself matches its counterpart in the\n                  pattern. The pattern match checking occurs from right to left\n                  in this method.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Let m and n be the lengths of pattern P and text T\n                  respectively. Initially the checking starts from m<sup\n                    >th</sup\n                  >\n                  position in T with m<sup>th</sup> position of P(last letter of\n                  P).\n                </li>\n                <li>\n                  <strong\n                    >General Possibilities (Say char ch1 from text is being\n                    compared with char ch2 from pattern).</strong\n                  >\n                  <ol class=\"pl-4\">\n                    <li>\n                      If ch1 doesn't occur in T then we can safely shift the\n                      pattern by it's length.\n                    </li>\n                    <li>\n                      If ch1 doesn't match with ch2 but it has occurrences in\n                      m-1 positions of P, then shift the pattern such that the\n                      rightmost occurrence of ch1 in the pattern aligns with the\n                      ch1 in the text.\n                    </li>\n                    <li>\n                      If ch1 matches with ch2 and other characters don't match\n                      and there are no other occurrences of ch1 in T then this\n                      case is similar to first case i.e. shift the pattern by\n                      it's entire length.\n                    </li>\n                    <li>\n                      If ch1 matches with ch2 and other characters don't match\n                      and there are other occurrences of ch1 in T then shift the\n                      pattern similar to case 2.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Shift Table:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Initialize 0 to all 128 characters in an array, table[].\n                    </li>\n                    <li>Loop from i=0 to m-1</li>\n                    <li>\n                      For the index, j=pattern[i]; assign m-i-1 in the table.\n                      That is table[ pattern[i] ] = m-i-1.\n                    </li>\n                    <span class=\"text-muted\"\n                      >This determines the amount of positions to shifted for\n                      each character in the pattern based on the right most\n                      occurrence of that character in the pattern</span\n                    >\n                  </ol>\n                </li>\n                <li>\n                  <strong>String Matching:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Loop from i=m-1 to n-1\n                      <ol class=\"pl-4\">\n                        <li>\n                          Loop until from k=0 to m-1. <br />\n                          If pattern[m-k-1]==text[i-k] <br />\n                          then increment k, <br />\n                          else break the loop. <br />\n                          (This itself is the comparison from right to left.)\n                        </li>\n                        <li>\n                          If k==m then the pattern was matched and the\n                          position(= i-m+1) can be returned.\n                        </li>\n                        <li>\n                          Else increment i by the value in table corresponding\n                          to the current character (from the text) being\n                          compared with the pattern.\n                        </li>\n                      </ol>\n                    </li>\n                    <li>\n                      If you arrived at this step then it means that the pattern\n                      wasn't found, so return -1.\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n\t\t\t<div class=\"my-2 p-2\">\n              <h4>Time Complexity: O(n*m)</h4>\n            </div>\n\t\t\t        <div class=\"my-2 p-2\">\n                <strong>Similar posts:</strong>\n                  <a href=\"/blogs/stringMatchingUsingBruteForceTechnique\">String Matching Using Brute Force Technique</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  In string matching algorithm, we check whether a text T\n                  contains a pattern P(length of T>P) i.e to check whether P is\n                  a substring of T or not. Since we are trying to check a fixed\n                  string P, sometimes these algorithms are called exact string\n                  matching algorithms.\n                </p>\n                <p>\n                  In horspool's method, we shift the pattern for matching by\n                  looking at the character(say c) of the text that is aligned\n                  against the last character of the pattern. This is the case\n                  even if character c itself matches its counterpart in the\n                  pattern. The pattern match checking occurs from right to left\n                  in this method.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Let m and n be the lengths of pattern P and text T\n                  respectively. Initially the checking starts from m<sup\n                    >th</sup\n                  >\n                  position in T with m<sup>th</sup> position of P(last letter of\n                  P).\n                </li>\n                <li>\n                  <strong\n                    >General Possibilities (Say char ch1 from text is being\n                    compared with char ch2 from pattern).</strong\n                  >\n                  <ol class=\"pl-4\">\n                    <li>\n                      If ch1 doesn't occur in T then we can safely shift the\n                      pattern by it's length.\n                    </li>\n                    <li>\n                      If ch1 doesn't match with ch2 but it has occurrences in\n                      m-1 positions of P, then shift the pattern such that the\n                      rightmost occurrence of ch1 in the pattern aligns with the\n                      ch1 in the text.\n                    </li>\n                    <li>\n                      If ch1 matches with ch2 and other characters don't match\n                      and there are no other occurrences of ch1 in T then this\n                      case is similar to first case i.e. shift the pattern by\n                      it's entire length.\n                    </li>\n                    <li>\n                      If ch1 matches with ch2 and other characters don't match\n                      and there are other occurrences of ch1 in T then shift the\n                      pattern similar to case 2.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Shift Table:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Initialize 0 to all 128 characters in an array, table[].\n                    </li>\n                    <li>Loop from i=0 to m-1</li>\n                    <li>\n                      For the index, j=pattern[i]; assign m-i-1 in the table.\n                      That is table[ pattern[i] ] = m-i-1.\n                    </li>\n                    <span class=\"text-muted\"\n                      >This determines the amount of positions to shifted for\n                      each character in the pattern based on the right most\n                      occurrence of that character in the pattern</span\n                    >\n                  </ol>\n                </li>\n                <li>\n                  <strong>String Matching:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Loop from i=m-1 to n-1\n                      <ol class=\"pl-4\">\n                        <li>\n                          Loop until from k=0 to m-1. <br />\n                          If pattern[m-k-1]==text[i-k] <br />\n                          then increment k, <br />\n                          else break the loop. <br />\n                          (This itself is the comparison from right to left.)\n                        </li>\n                        <li>\n                          If k==m then the pattern was matched and the\n                          position(= i-m+1) can be returned.\n                        </li>\n                        <li>\n                          Else increment i by the value in table corresponding\n                          to the current character (from the text) being\n                          compared with the pattern.\n                        </li>\n                      </ol>\n                    </li>\n                    <li>\n                      If you arrived at this step then it means that the pattern\n                      wasn't found, so return -1.\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n\t\t\t<div class=\"my-2 p-2\">\n              <h4>Time Complexity: O(n*m)</h4>\n            </div>\n\t\t\t        <div class=\"my-2 p-2\">\n                <strong>Similar posts:</strong>\n                  <a href=\"/blogs/stringMatchingUsingBruteForceTechnique\">String Matching Using Brute Force Technique</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M44%2095c-4%200-5%200-5%202h-1c-1-3-7-3-9%200-3%203-2%207%201%209%204%203%204%204%200%204-3-1-5%201-2%203%202%201%207%201%208-1%203-2%202-7%200-8l-2-2h2l3-1%202-1c2%200%202%201%202%207%200%207%200%207%202%207%203%200%203%200%203-7s1-8%203-8%202-1%202-2c-1-3-1-3-9-2m25%201l-1%2010v8h11v-8l2%204c2%203%202%204%204%204s3-1%203-10v-9h-2c-3%200-3%200-3%203v4l-1-4c-2-4-7-4-7%200-1%202-1%202-1-1%200-2%200-3-2-3s-3%201-3%202m80-1c-5%200-5%200-5%203l2%202c2%200%202%201%202%207%200%207%200%207%203%207%202%200%202%200%202-7%200-8%200-8%203-8%202%200%202%200%202-2-1-3-1-3-9-2m14%200c-5%203-7%2013-3%2017s11%201%2011-3c0-3-2-3-4-1l-2%201v-8c0-3%202-2%202%200%200%201%201%202%203%202%203%200%203-5%200-7s-4-2-7-1m11%204l-1%2010c0%205%200%205%202%205%203%200%203-1%203-4%201-5%204-6%204%200%200%203%200%204%202%204h3v-10c0-10%200-10-2-10s-3%201-3%204l-2%204-1-4c0-3-1-4-3-4-1%200-2%201-2%205m15-3l-1%2010v8h11v-8l2%204c1%203%202%204%204%204s2%200%202-4l1-10c0-5%200-5-2-5-3%200-3%200-3%203v4l-1-4c-2-3-6-4-7-1-1%201-1%201-1-1%200-3-5-3-5%200M54%2098l-1%2010c0%206%200%206%202%206%203%200%203%200%203-3l1-3%202%203c1%203%201%203%204%202%202-1%202-1%201-4-2-2-2-3%200-4v-9l-7-1h-5v3m37%200c-3%204-3%2014%201%2015%205%203%2012-4%209-8-2-3-5-2-6%202l-1%202c-1-2%200-9%202-10l1%201c0%201%201%202%203%202s3-2%201-5-7-3-10%201m20%201l-1%209c0%206%200%206%203%206l2-2c0-2%200-2%202-1s2%201%203-1c2-1%202-1%201%201v3h6l6-1c0-3%204-3%204-1l3%202c3%200%203%200%202-5-1-11-2-13-5-14-3%200-4%200-6%206l-3%209c0%202-1%201-1-6%200-8-1-9-3-9l-4%204-1%204-1-4c-2-5-7-6-7%200m100-1c-4%205-3%2015%201%2016%206%201%2012-6%209-10-3-3-8%201-6%205l-1%201-1-3c0-4%202-8%203-7v1l2%201c4%200%205-2%203-5s-7-3-10%201m88%208l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%205-17-1-20l-3-2-1-1-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/f426672aee7d3a2f7b61708559929237/ee604/blog68.png","srcSet":"/static/f426672aee7d3a2f7b61708559929237/69585/blog68.png 200w,\n/static/f426672aee7d3a2f7b61708559929237/497c6/blog68.png 400w,\n/static/f426672aee7d3a2f7b61708559929237/ee604/blog68.png 800w,\n/static/f426672aee7d3a2f7b61708559929237/f3583/blog68.png 1200w,\n/static/f426672aee7d3a2f7b61708559929237/e4d72/blog68.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"stringMatchingUsingHorspoolsAlgorithm","thumbnail":"thumbnails/blog68.png"}},"staticQueryHashes":["2987289216","63159454"]}