Leetcode-467. Unique Substrings in Wraparound String
来源:互联网 发布:sql多表查询 编辑:程序博客网 时间:2024/06/13 00:31
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
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: 1Explanation: Only the substring "a" of string "a" is in the string s.
Example 2:
Input: "cac"Output: 2Explanation: There are two substrings "a", "c" of string "cac" in the string s.
Example 3:
Input: "zab"Output: 6Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.看完论坛里关于这个题的解法,我只能说服了,和当初看到异或操作感觉是完全一样的感觉。。。
public class Solution { public int findSubstringInWraproundString(String p) { // count[i] is the maximum unique substring end with ith letter. // 0 - 'a', 1 - 'b', ..., 25 - 'z'. int[] count = new int[26]; // store longest contiguous substring ends at current position. int maxLengthCur = 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))) { maxLengthCur++; } else { maxLengthCur = 1; } int index = p.charAt(i) - 'a'; count[index] = Math.max(count[index], maxLengthCur); } // Sum to get result int sum = 0; for (int i = 0; i < 26; i++) { sum += count[i]; } return sum; }}
- Leetcode-467. Unique Substrings in Wraparound String
- Leetcode 467. Unique Substrings in Wraparound String
- 【LeetCode】467. Unique Substrings in Wraparound String
- Leetcode-467. Unique Substrings in Wraparound String
- [LeetCode]467. Unique Substrings in Wraparound String
- **LeetCode 467. Unique Substrings in Wraparound String
- 【LeetCode】467. Unique Substrings in Wraparound String
- LeetCode 467. Unique Substrings in Wraparound String
- [leetcode]467. Unique Substrings in Wraparound String
- [LeetCode] 467. Unique Substrings in Wraparound String
- Leetcode:467. Unique Substrings in Wraparound String
- leetcode 467. Unique Substrings in Wraparound String
- [LeetCode]Unique Substrings in Wraparound String
- [leetcode 467]Unique Substrings in Wraparound String
- Leetcode-Unique Substrings in Wraparound String
- leetcode Unique Substrings in Wraparound String
- [Leetcode.467]Unique Substrings in Wraparound String
- [LeetCode]Unique Substrings in Wraparound String
- Cow Checklist
- 那问大侠知道那边有关于OTA的资料下载,急用,谢谢!
- jedis操作zset数据结构总结
- java读取xml文件
- 科普一下,什么是网站系统的性能,可用性,可伸缩性,可扩展性?
- Leetcode-467. Unique Substrings in Wraparound String
- 如何给任意后缀名指定应用程序打开
- poj 2443 状态压缩 叉姐的魔法训练
- 100分求一简单思路
- 后台数据库操作(Python)
- [Leetcode] 41. First Missing Positive 解题报告
- HTML 颜色名
- Activity启动模式
- jedis操作set数据结构总结