unique-substrings-in-wraparound-string(好)

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

好,我自己做出来的。多总结规律,多思考。

package com.company;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int findSubstringInWraproundString(String p) {
        Map<Integer, Integer> mp = new HashMap<>();
        int ret = 0;
        int cur = 0;
        char[] array = p.toCharArray();
        if (array.length < 1) {
            return 0;
        }
        for (int i=0; i<26; i++) {
            mp.put(i, 0);
        }

        ret++;
        cur++;
        mp.put(array[0]-‘a‘, 1);
        for (int i=1; i<array.length; i++) {
            if ((array[i]-array[i-1]+26) % 26 == 1)  {
                cur++;
            }
            else {
                cur = 1;
            }

            if (cur > mp.get(array[i]-‘a‘)) {
                ret += cur - mp.get(array[i]-‘a‘);
                mp.put(array[i]-‘a‘, cur);
            }
        }
        return ret;
    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String p = "zab";

        Solution solution = new Solution();
        int ret  = solution.findSubstringInWraproundString(p);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}
时间: 2024-12-25 00:30:51

unique-substrings-in-wraparound-string(好)的相关文章

[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 封装字符串中的独特子字符串

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 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

动态规划-独特的子字符串存在于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 &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

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 >

Leetcode-387 First Unique Character in a String

#387.   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't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You m

LeetCode(387)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't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowerc

LeetCode_387. First Unique Character in a String

387. First Unique Character in a String Easy Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You