2018-09-01 22:50:59

问题描述:

问题求解:

如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上。

后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题。

  1. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example "abcd", the max number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
  2. 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.
  3. No matter how long is a contiguous substring in p, it is in s since s has infinite length.
  4. Now we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z' and those substrings are all in s. Summary is the answer, according to the question.
    public int findSubstringInWraproundString(String p) {int res = 0;int[] dp = new int[26];int maxLen = 0;for (int i = 0; i < p.length(); i++) {if (i > 0 && (p.charAt(i) - p.charAt(i - 1) == 1 || p.charAt(i - 1) - p.charAt(i) == 25)) {maxLen++;}else maxLen = 1;dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], maxLen);}for (int i = 0; i < 26; i++) res += dp[i];return res;}

转载于:https://www.cnblogs.com/hyserendipity/p/9572041.html

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

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

    Description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrst ...

  2. 算法作业系列10——Unique Substrings in Wraparound String

    算法作业系列(十) Unique Substrings in Wraparound String 写在前面 如果你是因为没有思路来找答案的,这里建议你去看看题目的s,因为s是一个连续字母的字符串,所以 ...

  3. Leetcode 467. Unique Substrings in Wraparound String

    题目链接:Unique Substrings in Wraparound String Consider the string s to be the infinite wraparound stri ...

  4. 467. Unique Substrings in Wraparound String

    467. Unique Substrings in Wraparound String 题目的意思是给一个无限循环的大字符串s,然后给一个小串p,求出p的子串在s中的个数: 首先可以将其分为26个状态 ...

  5. [LeetCode]Unique Substrings in Wraparound String@Python

    Unique Substrings in Wraparound String Consider the string s to be the infinite wraparound string of ...

  6. leetcode - 467. Unique Substrings in Wraparound String

    算法系列博客之Dynamic Programming 本篇博客将运用动态规划的思想来解决leetcode上467号问题 问题描述: Consider the string s to be the in ...

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

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

  8. LeetCode467. Unique Substrings in Wraparound String

    题目 Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz&qu ...

  9. Leetcode-467. Unique Substrings in Wraparound String

    前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流.因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法.如有错误欢迎指正.博主首发CSDN ...

最新文章

  1. python需要变量命名规则_和孩子一起学习python之变量命名规则
  2. Kafka是什么,JMS是什么,常见的类JMS消息服务器,为什么需要消息队列(来自学习笔记)
  3. 架构设计分布式数据结构与算法面试题
  4. mysql内置含糊_mySQL入门04 内置函数
  5. 开发 OpenAM Java 客户端
  6. LeetCode--80. 删除排序数组中的重复项Ⅱ(双指针,暴力)
  7. Java学习日报—消息队列—2021/11/23
  8. MTK 驱动(66)---Android recovery UI实现分析
  9. Eclipse中 Junit 正常运行完了 可是方法覆盖率全红 解决办法 (附带②EclEmma插件安装方法④覆盖率抽出与合并)
  10. 把ipad变成电脑的音箱
  11. Netscreen的岁月 from Sina
  12. 百度云API怎么使用
  13. 计算机网络无法连接共享打印机驱动,Windows 10 安装网络共享打印机失败,提示0x00009c4a 无法连接到打印机解决办法...
  14. 【verbs】ibv_modify_qp()|RDMA
  15. 用一个div绘制背景流动网格特效
  16. 单片机中常用的串口通信协议帧
  17. 前端ECMAScript6个人学习笔记
  18. 巴贝奇通用计算机设计思想称为,计算机有运算速度快精确度高有记忆和逻辑判断能力.docx...
  19. python中的pygame弹球游戏代码_python pygame实现挡板弹球游戏
  20. c#日期格式操作(贴吧转)

热门文章

  1. 在ArchLinux操作系统安装ctex宏包
  2. 介绍磁性微球在各领域中的应用
  3. 一文教会你Flask模板
  4. 读书笔记_《当下的力量》_精华书摘
  5. 只要你认真看完一万字☀️Linux操作系统基础知识☀️分分钟钟都吊打面试官《❤️记得收藏❤️》
  6. jdk8銝要onematch_JDK8新特性详解 - 纯粹而又极致的光--木九天 - OSCHINA - 中文开源技术交流社区...
  7. LabVIEW实现Modbus通信
  8. 【微信小程序入门到精通】— 小程序的 API 到底是个啥
  9. 基于DFA算法实现过滤敏感词
  10. 当代大学生提升自己的最佳捷径-python