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 题解(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
  • 2301

leetcode - 467. Unique Substrings in Wraparound String

算法系列博客之Dynamic Programming 本篇博客将运用动态规划的思想来解决leetcode上467号问题 问题描述:Consider the string s to be the i...
  • Quiteen
  • Quiteen
  • 2017年06月04日 17:28
  • 86

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

LeetCode 467 Unique Substrings in Wraparound String (思维)

[Leetcode.467]Unique Substrings in Wraparound String

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

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 ...
  • jmspan
  • jmspan
  • 2016年12月06日 05:32
  • 404

leetcode:Unique Substrings in Wraparound String

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

DP (4) -- Largest Divisible Subset, Coin Change, Unique Substrings in Wraparound String

Largest Divisible Subset,Coin Change,Unique Substrings in Wraparound String
  • dwt0317
  • dwt0317
  • 2017年01月08日 20:47
  • 79

LeetCode - First Unique Character in a String

题目 Given a string, find the first non-repeating character in it and return it’s index. If it doesn’...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode-467. Unique Substrings in Wraparound String
举报原因:
原因补充:

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