Leetcode-467. Unique Substrings in Wraparound String

原创 2017年01月02日 23:04:05

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发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: 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.
看完论坛里关于这个题的解法,我只能说服了,和当初看到异或操作感觉是完全一样的感觉。。。

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

问题描述Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s...

LeetCode 467 Unique Substrings in Wraparound String (思维题)

LeetCode 467 Unique Substrings in Wraparound String (思维)

LeetCode 题解(Week 10): 467. Unique Substrings in Wraparound String

原题目Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s ...

Leetcode 467. Unique Substrings in Wraparound String 子串问题 结题报告

1 解题思想这道题是说给了一个字符串P,需要从其中找到所有出现在S中的子串。 这里的S的子串是一个比较神奇的存在,起就是abcdedf..zabcde…这样无限迭代下去的一个串,也就是只要P的字符串...
  • MebiuW
  • MebiuW
  • 2016-12-05 20:42
  • 2077

leetcode - 467. Unique Substrings in Wraparound String

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

467. Unique Substrings in Wraparound String

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

467. Unique Substrings in Wraparound String

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

467. Unique Substrings in Wraparound String -Medium

Question Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz...

467. Unique Substrings in Wraparound String

原题网址:https://leetcode.com/problems/unique-substrings-in-wraparound-string/ Consider the string ...

leetcode:Unique Substrings in Wraparound String

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s wi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)