{"id":9741,"date":"2022-05-15T12:01:07","date_gmt":"2022-05-15T19:01:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9741"},"modified":"2022-05-15T12:02:53","modified_gmt":"2022-05-15T19:02:53","slug":"leetcode-2269-find-the-k-beauty-of-a-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2269-find-the-k-beauty-of-a-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2269. Find the K-Beauty of a Number"},"content":{"rendered":"\n<p>The&nbsp;<strong>k-beauty<\/strong>&nbsp;of an integer&nbsp;<code>num<\/code>&nbsp;is defined as the number of&nbsp;<strong>substrings<\/strong>&nbsp;of&nbsp;<code>num<\/code>&nbsp;when it is read as a string that meet the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It has a length of&nbsp;<code>k<\/code>.<\/li><li>It is a divisor of&nbsp;<code>num<\/code>.<\/li><\/ul>\n\n\n\n<p>Given integers&nbsp;<code>num<\/code>&nbsp;and&nbsp;<code>k<\/code>, return&nbsp;<em>the k-beauty of&nbsp;<\/em><code>num<\/code>.<\/p>\n\n\n\n<p>Note:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Leading zeros<\/strong>&nbsp;are allowed.<\/li><li><code>0<\/code>&nbsp;is not a divisor of any value.<\/li><\/ul>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters in a string.<\/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> num = 240, k = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The following are the substrings of num of length k:\n- \"24\" from \"<strong><u>24<\/u><\/strong>0\": 24 is a divisor of 240.\n- \"40\" from \"2<strong>40<\/strong>\": 40 is a divisor of 240.\nTherefore, the k-beauty is 2.\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> num = 430043, k = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The following are the substrings of num of length k:\n- \"43\" from \"<strong>43<\/strong>0043\": 43 is a divisor of 430043.\n- \"30\" from \"4<strong>30<\/strong>043\": 30 is not a divisor of 430043.\n- \"00\" from \"43<strong>00<\/strong>43\": 0 is not a divisor of 430043.\n- \"04\" from \"430<strong>04<\/strong>3\": 4 is not a divisor of 430043.\n- \"43\" from \"4300<strong>43<\/strong>\": 43 is a divisor of 430043.\nTherefore, the k-beauty is 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= num &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= num.length<\/code>&nbsp;(taking&nbsp;<code>num<\/code>&nbsp;as a string)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Substring<\/strong><\/h2>\n\n\n\n<p>Note: the substring can be 0, e.g. &#8220;00&#8221;<\/p>\n\n\n\n<p>Time complexity: O((l-k)*k)<br>Space complexity: O(l + k) -&gt; 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  int divisorSubstrings(int num, int k) {\n    string s = to_string(num);\n    int ans = 0;\n    for (int i = 0; i <= s.length() - k; ++i) {\n      const int t = stoi(s.substr(i, k));\n      ans += t &#038;&#038; (num % t == 0);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;k-beauty&nbsp;of an integer&nbsp;num&nbsp;is defined as the number of&nbsp;substrings&nbsp;of&nbsp;num&nbsp;when it is read as a string that meet the following conditions: It has a length of&nbsp;k. It&#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-9741","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\/9741","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=9741"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9741\/revisions"}],"predecessor-version":[{"id":9743,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9741\/revisions\/9743"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}