Leetcode: Unique Substrings in Wraparound String
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.
这道题我一开始发现了数学规律:
length of consecutive chars vs number of different substring
“a” -> 1 = 1
"ab" -> 3 = 1+2
"abc" -> 6 = 1+2+3
"abcd" -> 10 = 1+2+3+4
于是打算计算string p里面每一段consecutive substring的length是多少,比如abcxyz, 两段分别为“abc”“xyz”, len分别为3和3,#of substring分别是6和6,总数是6+6。 但是这个方法没法处理有重复的,比如abczab,"abc"依然能产生6个substring, 然而"zab"就有重复了,只能产生3个different substring.
如何处理重复呢? “abc”和“zab”里面的"ab"要同时考虑到,于是就有了DP的想法:
total number of different substring = sum of {number of different substring end by each char}
Further,
number of different substring end by each char = length of longest contiguous substring ends with that letter.
Example "abcd", the number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example:"abcdbcd", the max number of unique substring ends with 'd' is 4 and all substrings formed by the 2nd "bcd" part are covered in the 4 substrings already.
public class Solution {
public int findSubstringInWraproundString(String p) {
if (p==null || p.length()==0) return 0;
int[] nums = new int[26]; //numbers of different substrings end with a specific char
int longestConsec = 0;
for (int i=0; i<p.length(); i++) {
if (i>0 && (p.charAt(i-1)+1 == p.charAt(i) || p.charAt(i-1)-25 == p.charAt(i))) {
longestConsec++;
}
else longestConsec = 1;
char c = p.charAt(i);
nums[(int)(c-'a')] = Math.max(nums[(int)(c-'a')], longestConsec);
}
int res = 0;
for (int num : nums) {
res += num;
}
return res;
}
}
Leetcode: Unique Substrings in Wraparound String的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467. [leetcode] Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- LeetCode 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String
2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
随机推荐
- jsonp模拟获取百度搜索相关词汇
随便写了个jsonp模拟百度搜索相关词汇的小demo,帮助新手理解jsonp的用法. <!DOCTYPE html><html lang="en">< ...
- MongoDB-C#驱动基本操作
#region IMongoQuery //Query.EQ("", val);//字段值=val //Query.NE("", val);//字段值!=val ...
- Struts2二级菜单联动
http://www.cnblogs.com/wujixing/p/5194461.html ps: Java面试 http://blog.csdn.net/zhang070809/article/d ...
- 李洪强iOS经典面试题140-UI
李洪强iOS经典面试题140-UI UI viewcontroller的一些方法的说明viewDidLoad,viewWillDisappear, viewWillAppear方法的 顺序和作用? ...
- PHP入门介绍与环境配置
浏览目录: 一.PHP的背景和优势: 二.PHP原理简介: 三.PHP运行环境配置: 四.编写简单的PHP代码以及测试. 一.PHP的背景和优势 1.1 什么是PHP? PHP是能让你生成动态网页 ...
- Win7 下安装VirtualBox 没有Ubuntu 64bit 选项问题
参考: win7安装virtualbox遇到的问题 基于VirtualBox虚拟机安装Ubuntu图文教程 问题 在安装VirtualBox之后,选择虚拟机进行安装的时候发现没有Ubuntu 64bi ...
- 关于iis8.5中发布的网站无法连接数据库的解决方案。
发布的网站在浏览时出现如下提示: “/”应用程序中的服务器错误. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL ...
- AI PRO I 第4章
Behavior Selection Algorithms An Overview Michael Dawe, Steve Gargolinski, Luke Dicken, Troy Humphre ...
- 开源代码分析之Android/iOS Hybrid JSBridge框架
Hybrid开发是现在的主流形式,对于业务快速迭代的公司尤其重要.曾将在鞋厂接触了很多关于Hybrid的理念,在这里分享一些Hybrid框架思想. Hybrid框架包括Native与H5的通信,Web ...
- 如何计算合适的InnoDB的(innodb_log_file_size)日志文件大小
在mysql工具中如phpmyadmin中执行show engine innodb status;注意观察Log sequence number 60秒后再次执行 获取Log sequence num ...