您的位置:首页 > 健康 > 美食 > 如何开网店具体步骤_比较好的搜索引擎_长沙seo袁飞_山东移动网站建设

如何开网店具体步骤_比较好的搜索引擎_长沙seo袁飞_山东移动网站建设

2024/12/23 23:34:45 来源:https://blog.csdn.net/sinat_41679123/article/details/144639746  浏览:    关键词:如何开网店具体步骤_比较好的搜索引擎_长沙seo袁飞_山东移动网站建设
如何开网店具体步骤_比较好的搜索引擎_长沙seo袁飞_山东移动网站建设

Description

Each character of the English alphabet has been mapped to a digit as shown below.

A string is divisible if the sum of the mapped values of its characters is divisible by its length.

Given a string s, return the number of divisible substrings of s.

A substring is a contiguous non-empty sequence of characters within a string.

Example 1:

在这里插入图片描述

Input: word = "asdf"
Output: 6
Explanation: The table above contains the details about every substring of word, and we can see that 6 of them are divisible.

Example 2:

Input: word = "bdh"
Output: 4
Explanation: The 4 divisible substrings are: "b", "d", "h", "bdh".
It can be shown that there are no other substrings of word that are divisible.

Example 3:

Input: word = "abcd"
Output: 6
Explanation: The 6 divisible substrings are: "a", "b", "c", "d", "ab", "cd".
It can be shown that there are no other substrings of word that are divisible.

Constraints:

1 <= word.length <= 2000
word consists only of lowercase English letters.

Solution

Simulation

Since the word length is not large, we could just generate all the substrings and see if its value is divisible by the length.

Time complexity: o ( n 2 ) o(n^2) o(n2)
Space complexity: o ( 1 ) o(1) o(1)

Prefix sum

Still didn’t get it after help…

Code

Simulation

class Solution:def countDivisibleSubstrings(self, word: str) -> int:chr_num = {'a': 1, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 3, 'g': 3, 'h': 3, 'i': 4,'j':4,'k':4,'l':5,'m':5,'n':5,'o':6,'p':6,'q':6,'r':7,'s':7,'t':7,'u':8,'v':8,'w':8,'x':9,'y':9,'z':9}res = 0for i in range(len(word)):cur_sum = 0for j in range(i, len(word)):cur_sum += chr_num[word[j]]if cur_sum % (j - i + 1) == 0:res += 1return res

Prefix sum

class Solution:def countDivisibleSubstrings(self, word: str) -> int:chr_num = {'a': 1, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 3, 'g': 3, 'h': 3, 'i': 4,'j':4,'k':4,'l':5,'m':5,'n':5,'o':6,'p':6,'q':6,'r':7,'s':7,'t':7,'u':8,'v':8,'w':8,'x':9,'y':9,'z':9}res = 0for i in range(1, 10):pre_sum = {0: 1}cur_sum = 0for word_i, each_word in enumerate(word):cur_sum += chr_num[each_word] - ires += pre_sum.get(cur_sum, 0)pre_sum[cur_sum] = pre_sum.get(cur_sum, 0) + 1return res

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com