作者: 负雪明烛
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 日 —— 下雨天还是挺舒服的

【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)的更多相关文章

  1. LeetCode 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  2. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  3. 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  4. 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...

  5. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. Leetcode: Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  7. [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  8. 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String

    2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...

  9. 467. [leetcode] Unique Substrings in Wraparound String

    467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...

随机推荐

  1. 24-Longest Palindromic Substring-Leetcode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. Scrapy爬虫框架的安装和使用

    Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...

  3. HMS Core Discovery直播预告 | AI画质增强 ,开启超清视界

    [直播入口] B站华为开发者联盟:http://live.bilibili.com/22551651 4K.8K视频屡见不鲜,HD.FHD分辨率成小屏标配,当网络卡顿.视频自动切换到较低画质时,用户最 ...

  4. Oracle LOB类型

    一.Oracle中的varchar2类型1.我们在Oracle数据库存储的字符数据一般是用VARCHAR2.VARCHAR2既分PL/SQL Data Types中的变量类型,也分Oracle Dat ...

  5. 远程连接mysql库问题

    如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL serve ...

  6. Spring Boot的异步任务、定时任务和邮件任务

    一.异步任务 1.启动类添加注解@EnableAsync,开启异步任务注解功能: 2.需要异步执行的方法上添加@Async注解. 二.定时任务 1.启动类添加注解@EnableScheduling,开 ...

  7. 会话-cookie

    package com.hopetesting.cookie;import javax.servlet.ServletException;import javax.servlet.annotation ...

  8. Spring中Bean的装配方式

    一.基于xml的装配 Student.java package com.yh; public class Student implements People { public void breath( ...

  9. 【C/C++】从矩阵左上角走到右下角

    tx的笔试,但是只过了10%,就离谱 #include <bits/stdc++.h> using namespace std; const int maxn = 1010; long d ...

  10. 利用代码覆盖率提高嵌入式软件的可靠性 - VectorCAST

    简介 代码覆盖率是衡量软件测试完成情况的指标,通常基于测试过程中已检查的程序源代码比例 计算得出.代码覆盖率可以有效避免包含未测试代码的程序被发布. 代码覆盖率能不能提高软件的可靠性?答案是肯定的,代 ...