467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

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

C++:

class Solution {
public:
    int findSubstringInWraproundString(string p)
    {
        vector<int> cnt(26, 0);
        int len = 0;
        for (int i = 0; i < p.size(); ++i)
        {
            if (i > 0 && (p[i] == p[i - 1] + 1 || p[i - 1] - p[i] == 25))
            {
                ++len;
            }
            else
            {
                len = 1;
            }
            cnt[p[i] - ‘a‘] = max(cnt[p[i] - ‘a‘], len);
        }
        return accumulate(cnt.begin(), cnt.end(), 0);
    }
};

参考:https://www.cnblogs.com/grandyang/p/6143071.html

原文地址:https://www.cnblogs.com/xidian2014/p/8902548.html

时间: 2024-07-31 15:58:43

467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串的相关文章

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

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

467. Unique Substrings in Wraparound String

https://leetcode.com/problems/unique-substrings-in-wraparound-string/#/description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefgh

LeetCode &quot;467. Unique Substrings in Wraparound String&quot; !!

Check out this brilliant solution:https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp My first solution was a O(n^2) typical DP boiler-plate code. The key is, that solution is too generalized. With given char as the ending char in

Leetcode 467.环绕字符串中的唯一子字符串

环绕字符串中的唯一子字符串 把字符串 s 看作是"abcdefghijklmnopqrstuvwxyz"的无限环绕字符串,所以 s 看起来是这样的:"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". 现在我们有了另一个字符串 p .你需要的是找出 s 中有多少个唯一的 p 的非空子串,尤其是当你的输入是字符串 p ,你需要输出字符串 s 中 p 的不同的非空子串的数目. 注意: p 

输入一个字符串,输出该字符串中对称的子字符串的最大长度。

public class LongestSymmtricalLength2 { /* * Q75题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度. * 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. */ public static void main(String[] args) { String[] strs = { "a","google", "elgoog", "agol

java统计字符串中字符及子字符串个数

import java.util.Scanner;public class Counter { static Scanner scanner = new Scanner(System.in); public static void count(String s) { int low, upper, num, others; low = upper = num = others = 0; for (int i = 0; i < s.length(); i++) { if (Character.is

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

2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that

[Leetcode] DP-- 467. Unique Substrings in Wraparound String

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

LeetCode Unique Substrings in Wraparound String

原题链接在这里:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ 题目: Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvw