1. Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".
  2.  
  3. 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.
  4.  
  5. Note: p consists of only lowercase English letters and the size of p might be over 10000.
  6.  
  7. Example 1:
  8. Input: "a"
  9. Output: 1
  10.  
  11. Explanation: Only the substring "a" of string "a" is in the string s.
  12. Example 2:
  13. Input: "cac"
  14. Output: 2
  15. Explanation: There are two substrings "a", "c" of string "cac" in the string s.
  16. Example 3:
  17. Input: "zab"
  18. Output: 6
  19. 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.

  1. public class Solution {
  2. public int findSubstringInWraproundString(String p) {
  3. if (p==null || p.length()==0) return 0;
  4. int[] nums = new int[26]; //numbers of different substrings end with a specific char
  5.  
  6. int longestConsec = 0;
  7. for (int i=0; i<p.length(); i++) {
  8. if (i>0 && (p.charAt(i-1)+1 == p.charAt(i) || p.charAt(i-1)-25 == p.charAt(i))) {
  9. longestConsec++;
  10. }
  11. else longestConsec = 1;
  12.  
  13. char c = p.charAt(i);
  14. nums[(int)(c-'a')] = Math.max(nums[(int)(c-'a')], longestConsec);
  15. }
  16.  
  17. int res = 0;
  18. for (int num : nums) {
  19. res += num;
  20. }
  21. return res;
  22. }
  23. }

Leetcode: Unique Substrings in Wraparound String的更多相关文章

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

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

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

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

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

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

  4. LeetCode 467. Unique Substrings in Wraparound String

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

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

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

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

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

  7. 467. 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 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

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

随机推荐

  1. BestCoder Round #85(ZOJ1569尚未验证)

    A题 子序列和啊,就要想到前缀和的差.这个转换一定要!记着!那么i到j的一段子序列和Sij%m ==  0就等价于(Sj-Si-1)%m == 0 了,那么什么意思呢?就是如果有两段前缀和%m的模是一 ...

  2. Java 实现文件上传、下载、打包、文件copy、文件夹copy。

    文件and文件夹copy package org.test; import java.io.*; public class FileCopy { /** * 复制单个文件 * * @param old ...

  3. html和css书写规范

    HTML 规范 分离的标记.样式和脚本 结构.表现.行为分离 在可能情况下验证你的标记 使用编辑器验证你的标记是否正确,一般编辑器都自带有这个功能. 技术不支持的时候使用备胎,如canvas 编码格式 ...

  4. 【POJ3254】Corn Fields 状压DP第一次

    !!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...

  5. Android sdk资源包res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)

    (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x ...

  6. .Net 4.5 的async 和await 的简单理解使用

    原文地址:http://www.cnblogs.com/HJL-Blog/p/4432632.html 所谓的异步编程是利用CPU空闲时间和多核的特性,它所返回的Task或Task<TResul ...

  7. javascript平时例子⑩(表情发送)

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  8. BSBuDeJie_01

    一. 基本配置 1 项目图标 将图片直接拖入Assets-AppIcon 2 启动图片     3 软件名称   4 删除Main.stroryboard   5 设置窗口的根控制器 - (BOOL) ...

  9. JsonTest

    以前用MVC写网站时并不用考虑Json的转换,MVC已经提供了现成的方法. 现在没有用MVC,我就在考虑如何自己转换Json,想来想去自己写还是不够完美,于是尝试了一些其他人写的方法,尝试过微软提供的 ...

  10. PHP基础知识之数组

    数组的定义: array( key => value , ... ) // 键(key)可以是一个整数或字符串,键可以省略,默认从0开始索引 // 值(value)可以是任意类型的值或者简写的方 ...