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 ...
随机推荐
- BestCoder Round #85(ZOJ1569尚未验证)
A题 子序列和啊,就要想到前缀和的差.这个转换一定要!记着!那么i到j的一段子序列和Sij%m == 0就等价于(Sj-Si-1)%m == 0 了,那么什么意思呢?就是如果有两段前缀和%m的模是一 ...
- Java 实现文件上传、下载、打包、文件copy、文件夹copy。
文件and文件夹copy package org.test; import java.io.*; public class FileCopy { /** * 复制单个文件 * * @param old ...
- html和css书写规范
HTML 规范 分离的标记.样式和脚本 结构.表现.行为分离 在可能情况下验证你的标记 使用编辑器验证你的标记是否正确,一般编辑器都自带有这个功能. 技术不支持的时候使用备胎,如canvas 编码格式 ...
- 【POJ3254】Corn Fields 状压DP第一次
!!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...
- Android sdk资源包res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
(1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x ...
- .Net 4.5 的async 和await 的简单理解使用
原文地址:http://www.cnblogs.com/HJL-Blog/p/4432632.html 所谓的异步编程是利用CPU空闲时间和多核的特性,它所返回的Task或Task<TResul ...
- javascript平时例子⑩(表情发送)
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- BSBuDeJie_01
一. 基本配置 1 项目图标 将图片直接拖入Assets-AppIcon 2 启动图片 3 软件名称 4 删除Main.stroryboard 5 设置窗口的根控制器 - (BOOL) ...
- JsonTest
以前用MVC写网站时并不用考虑Json的转换,MVC已经提供了现成的方法. 现在没有用MVC,我就在考虑如何自己转换Json,想来想去自己写还是不够完美,于是尝试了一些其他人写的方法,尝试过微软提供的 ...
- PHP基础知识之数组
数组的定义: array( key => value , ... ) // 键(key)可以是一个整数或字符串,键可以省略,默认从0开始索引 // 值(value)可以是任意类型的值或者简写的方 ...