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.

分析

letters[i]表示 ‘a’+i结尾的最长子链。len记录的是以p[i]结尾的当时最长子链。如果len<=letters[curr] 则表示这些子链已经重复,不用加到res上。

其实只需要记录子链中以26个字母结尾的长度分别是多少,最后把它们加起来就可以了

class Solution {
public:
int findSubstringInWraproundString(string p) {
vector<int> letters(26, 0);
int res = 0, len = 0;
for (int i = 0; i < p.size(); i++) {
int cur = p[i] - 'a';
if (i > 0 && p[i - 1] != (cur + 26 - 1) % 26 + 'a') len = 0;
if (++len > letters[cur]) {
res += len - letters[cur];
letters[cur] = len;
}
}
return res;
}
};

LeetCode 467. Unique Substrings in Wraparound String的更多相关文章

  1. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  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. 1-18String类简介

    字符串(String)的不可变性 String类在java.lang包下面,是Object类的直接子类,通过API或者源码可以看到,String类是final修饰的,这说明String类不能被继承. ...

  2. Java-注解(不包含元注解)

    @SuppressWarnings:抑制警告 package java.lang; import java.lang.annotation.*; import static java.lang.ann ...

  3. [ CQOI 2018 ] 异或序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...

  4. 【学习笔记】比特(bit)、字,字节(B)存储单位之间的关系+其与操作系统位数的关系+不同编译器编译方式下数据类型的表示范围

    1.在表示网络传输速度中与表示存储单位的不同: 表示存储单位时:1kB=1024B,但在网络中表示传输速度是1KB=1000B 2.下面介绍表示存储单位时的关系及其与操作系统位数的关系: 1B=8bi ...

  5. Vue 2.0入门基础知识之内部指令

    1.Vue.js介绍 当前前端三大主流框架:Angular.React.Vue.React前段时间由于许可证风波,使得Vue的热度蹭蹭地上升.另外,Vue友好的API文档更是一大特色.Vue.js是一 ...

  6. 基于 Web 的 Go 语言 IDE - Wide 1.5.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE. 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈.Lint 实时运行:极速编译. ...

  7. coursera网站中的VTT字幕的使用

    coursera网站中的VTT字幕的使用 1.https://www.coursera.org/learn/os-virtsecurity/lecture/xuWgP/1-3-cao-zuo-xi-t ...

  8. RecyclerView 缓存机制学习笔记2

    RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...

  9. springboot的多个配置文件的关系

    一般我们在使用springboot时会用到多个环境下的配置文件,例如 application-dev.yml:开发环境 application-uat.yml:用户验收测试环境 application ...

  10. Python 循环结构语句

    1.for循环:计次循环 2.while循环:条件循环 3.嵌套循环 4.跳转语句 一.for循环的使用 1.进行数值循环 利用数值循环输出三次‘你好’: >>> for i in ...