作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/

题目描述:

Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".

Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s.

Note: p consists of only lowercase English letters and the size of p might be over 10000.

Example 1:

Input: "a"
Output: 1 Explanation: Only the substring "a" of string "a" is in the string s.

Example 2:

Input: "cac"
Output: 2
Explanation: There are two substrings "a", "c" of string "cac" in the string s.

Example 3:

Input: "zab"
Output: 6
Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.

题目大意

在一个无限循环的字母表s中,找出给定的字符串的所有子串在s中出现了多少次。

解题方法

这个做法和前几天的某个做法一致,从头开始遍历,在遍历的过程中,只用考虑当新添加这个字符的时候,能否和前面构成连续的,如果能构成连续的,那么结果中增加上以这个字符结尾的子串个数,即当前的长度。否则就是一个新串,长度是1.

其实这个思路就是dp,dp数组保存的是以当前字符结尾,能够成的所有子字符串个数。

比如abcd这个字符串,以d结尾的子字符串有abcd, bcd, cd,
d,那么我们可以发现bcd或者cd这些以d结尾的字符串的子字符串都包含在abcd中,那么我们知道以某个字符结束的最大字符串包含其他以该字符结束的字符串的所有子字符串。

这个思路又有点类似于虫取法,一直更新和保存子区间的长度。

时间复杂度是O(N),空间复杂度是O(26)。

class Solution:
def findSubstringInWraproundString(self, p):
"""
:type p: str
:rtype: int
"""
count = collections.defaultdict(int)
N = len(p)
_len = 0
for i in range(N):
if i > 0 and (ord(p[i]) - ord(p[i - 1]) == 1 or (p[i] == 'a' and p[i - 1] == 'z')):
_len += 1
else:
_len = 1
count[p[i]] = max(count[p[i]], _len)
return sum(count.values())

参考资料:

http://www.cnblogs.com/grandyang/p/6143071.html

日期

2018 年 10 月 16 日 —— 下雨天还是挺舒服的

最新文章

  1. LAMP之准备,samba搭建
  2. C++基础知识易错点总结(2)
  3. Linux grep与正则表达式
  4. React Native初试:Windows下Andriod环境搭建
  5. error while loading shared libraries: /usr/lib64/libc.so.6: invalid ELF header
  6. DataGridView编辑实时生效和索引-1没有值问题
  7. C#代码计时
  8. C#的匿名委托 和 Java的匿名局部内部类
  9. Java 小记 — RabbitMQ 的实践与思考
  10. Android下用Activity实现圆角的自定义弹窗
  11. Elasticsearch 通关教程(三): 索引别名Aliases问题
  12. axios拦截器
  13. C# 给一个控件去掉焦点
  14. A1110. Complete Binary Tree
  15. 用嵌入式块RAM IP核配置一个双口RAM
  16. Intent 传递数据
  17. 【SqlServer】SqlServer的游标使用
  18. 【转】iframe页面跳转时,导致父页面滚动!该怎么解决?
  19. CCommandLineInfo类
  20. python 初步认识Flask

热门文章

  1. MapReduce04 框架原理Shuffle
  2. Spark(二)【sc.textfile的分区策略源码分析】
  3. 【leetcode】1217. Minimum Cost to Move Chips to The Same Position
  4. 【leetcode】797. All Paths From Source to Target
  5. jenkins之分布式
  6. oracle keep
  7. Default Constructors
  8. RAC常见的宏
  9. 通过静态分析和持续集成 保证代码的质量 (Helix QAC)2
  10. HTTPS及流程简析