{"id":9967,"date":"2023-02-25T22:07:28","date_gmt":"2023-02-26T06:07:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9967"},"modified":"2023-02-25T22:07:36","modified_gmt":"2023-02-26T06:07:36","slug":"leetcode-2575-find-the-divisibility-array-of-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2575-find-the-divisibility-array-of-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2575.\u00a0Find the Divisibility Array of a String"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>word<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;consisting of digits, and a positive integer&nbsp;<code>m<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>divisibility array<\/strong>&nbsp;<code>div<\/code>&nbsp;of&nbsp;<code>word<\/code>&nbsp;is an integer array of length&nbsp;<code>n<\/code>&nbsp;such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>div[i] = 1<\/code>&nbsp;if the&nbsp;<strong>numeric value<\/strong>&nbsp;of&nbsp;<code>word[0,...,i]<\/code>&nbsp;is divisible by&nbsp;<code>m<\/code>, or<\/li><li><code>div[i] = 0<\/code>&nbsp;otherwise.<\/li><\/ul>\n\n\n\n<p>Return<em>&nbsp;the divisibility array of<\/em><em>&nbsp;<\/em><code>word<\/code>.<\/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> word = \"998244353\", m = 3\n<strong>Output:<\/strong> [1,1,0,0,0,1,1,0,0]\n<strong>Explanation:<\/strong> There are only 4 prefixes that are divisible by 3: \"9\", \"99\", \"998244\", and \"9982443\".\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> word = \"1010\", m = 10\n<strong>Output:<\/strong> [0,1,0,1]\n<strong>Explanation:<\/strong> There are only 2 prefixes that are divisible by 10: \"10\", and \"1010\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>word.length == n<\/code><\/li><li><code>word<\/code>&nbsp;consists of digits from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>9<\/code><\/li><li><code>1 &lt;= m &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Big Integer Math<\/strong><\/h2>\n\n\n\n<p>r = (r * 10 + word[i]) % m<\/p>\n\n\n\n<p>Time complexity: O(n)<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  vector<int> divisibilityArray(string word, int m) {\n    vector<int> ans;\n    long long r = 0;\n    for (char c : word) {\n      r = (r * 10 + c - '0') % m;\n      ans.push_back(r == 0);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;string&nbsp;word&nbsp;of length&nbsp;n&nbsp;consisting of digits, and a positive integer&nbsp;m. The&nbsp;divisibility array&nbsp;div&nbsp;of&nbsp;word&nbsp;is an integer array of length&nbsp;n&nbsp;such that: div[i] = 1&nbsp;if the&nbsp;numeric value&nbsp;of&nbsp;word[0,&#8230;,i]&nbsp;is divisible&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[347,31],"class_list":["post-9967","post","type-post","status-publish","format-standard","hentry","category-math","tag-big-integer","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9967","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=9967"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9967\/revisions"}],"predecessor-version":[{"id":9969,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9967\/revisions\/9969"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}