zoukankan      html  css  js  c++  java
  • 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 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.
    

    Approach #1: DFS. [C++] [TEL]

    class Solution {
    public:
        int findSubstringInWraproundString(string p) {
            string temp = "zabcdefghijklmnopqrstuvwxy";
            for (int i = 0; i < 10; ++i) temp += temp;
            set<string> ans;
            
            helper(ans, temp, p, 1);
            
            return ans.size();
        }
        
        void helper(set<string>& ans, string& temp, string p, int level) {
            if (level > p.length()) return ;
            
            for (int i = 0; i <= p.length() - level; ++i) {
                string subString = p.substr(i, level);
                int found = temp.find(subString);
                if (found != string::npos) {
                    ans.insert(subString);
                    //cout << subString << endl;
                }
            }
            
            helper(ans, temp, p, level+1);
        }
    };
    

      

    Approach #2: DP. [Java] [AC]

    class Solution {
        public int findSubstringInWraproundString(String p) {
            int[] count = new int[26];
            int curMaxLen = 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))
                    curMaxLen++;
                else
                    curMaxLen = 1;
                
                int index = p.charAt(i) - 'a';
                count[index] = Math.max(count[index], curMaxLen);
            }
            
            int sum = 0;
            for (int i = 0; i < 26; ++i) 
                sum += count[i];
            
            return sum;
        }
    }
    

      

    Analysis:

    The idea is, if we know the max number of unique substrings in p ends with 'a', 'b', 'c' ...... 'z', then the summary of them is the answer.

    • The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example "abcd", the max number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
    • If they are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example: "abcdbcd", the max number of unique substring ends with 'd' is 4 and all substrings formed by the 2nd "bcd" part are covered in the 4 substrings already.
    • No matter how long is a contiguous substring in p, it is in s since s has infinite length.
    • Now we know the max number of unique substrings in p ends with 'a', 'b', ......., 'z' and those substrings are all in s. Summary is the answer, according to the question.

    Reference:

    https://leetcode.com/problems/unique-substrings-in-wraparound-string/discuss/95439/Concise-Java-solution-using-DP

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    C# 虚方法virtual详解
    悟透javascript中的function
    C#并行编程-Task
    C#编程高并发的几种处理方法
    【CG】CG标准函数库——数学函数
    【Unity Shaders】学习笔记——SurfaceShader(八)生成立方图
    【Unity Shaders】学习笔记——SurfaceShader(七)法线贴图
    【Unity Shaders】学习笔记——SurfaceShader(六)混合纹理
    【Unity Shaders】学习笔记——SurfaceShader(五)让纹理动起来
    【Unity Shaders】学习笔记——SurfaceShader(四)用纹理改善漫反射
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10486617.html
Copyright © 2011-2022 走看看