How to find smallest substring which contains all characters from a given string?

后端 未结 15 1039
灰色年华
灰色年华 2020-12-02 07:36

I have recently come across an interesting question on strings. Suppose you are given following:

Input string1: \"this is a test string\"
Input strin         


        
相关标签:
15条回答
  • 2020-12-02 08:25

    C# Implementation:

    public static Tuple<int, int> FindMinSubstringWindow(string input, string pattern)
    {
        Tuple<int, int> windowCoords = new Tuple<int, int>(0, input.Length - 1);
        int[] patternHist = new int[256];
        for (int i = 0; i < pattern.Length; i++)
        {
            patternHist[pattern[i]]++;
        }
        int[] inputHist = new int[256];
        int minWindowLength = int.MaxValue;
        int count = 0;
        for (int begin = 0, end = 0; end < input.Length; end++)
        {
            // Skip what's not in pattern.
            if (patternHist[input[end]] == 0)
            {
                continue;
            }
            inputHist[input[end]]++;
            // Count letters that are in pattern.
            if (inputHist[input[end]] <= patternHist[input[end]])
            {
                count++;
            }
            // Window found.
            if (count == pattern.Length)
            {
                // Remove extra instances of letters from pattern
                // or just letters that aren't part of the pattern
                // from the beginning.
                while (patternHist[input[begin]] == 0 ||
                       inputHist[input[begin]] > patternHist[input[begin]])
                {
                    if (inputHist[input[begin]] > patternHist[input[begin]])
                    {
                        inputHist[input[begin]]--;
                    }
                    begin++;
                }
                // Current window found.
                int windowLength = end - begin + 1;
                if (windowLength < minWindowLength)
                {
                    windowCoords = new Tuple<int, int>(begin, end);
                    minWindowLength = windowLength;
                }
            }
        }
        if (count == pattern.Length)
        {
            return windowCoords;
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-12-02 08:26

    I've implemented it using Python3 at O(N) efficiency:

    def get(s, alphabet="abc"):
        seen = {}
        for c in alphabet:
            seen[c] = 0
        seen[s[0]] = 1
        start = 0
        end = 0
        shortest_s = 0
        shortest_e = 99999
        while end + 1 < len(s):
            while seen[s[start]] > 1:
                seen[s[start]] -= 1
                start += 1
            # Constant time check:
            if sum(seen.values()) == len(alphabet) and all(v == 1 for v in seen.values()) and \
                    shortest_e - shortest_s > end - start:
                shortest_s = start
                shortest_e = end
            end += 1
            seen[s[end]] += 1
        return s[shortest_s: shortest_e + 1]
    
    
    print(get("abbcac")) # Expected to return "bca"
    
    0 讨论(0)
  • 2020-12-02 08:29
        String s = "xyyzyzyx";
        String s1 = "xyz";
        String finalString ="";
        Map<Character,Integer> hm = new HashMap<>();
        if(s1!=null && s!=null && s.length()>s1.length()){
            for(int i =0;i<s1.length();i++){
                if(hm.get(s1.charAt(i))!=null){
                    int k = hm.get(s1.charAt(i))+1;
                    hm.put(s1.charAt(i), k);
                }else
                    hm.put(s1.charAt(i), 1);
            }
            Map<Character,Integer> t = new HashMap<>();
            int start =-1;
             for(int j=0;j<s.length();j++){
                 if(hm.get(s.charAt(j))!=null){
                     if(t.get(s.charAt(j))!=null){
                         if(t.get(s.charAt(j))!=hm.get(s.charAt(j))){
                         int k = t.get(s.charAt(j))+1;
                            t.put(s.charAt(j), k);
                         }
                     }else{
                         t.put(s.charAt(j), 1);
                         if(start==-1){
                             if(j+s1.length()>s.length()){
                                 break;
                             }
                             start = j;
                         }
                     }
                     if(hm.equals(t)){
                        t = new HashMap<>();
                        if(finalString.length()<s.substring(start,j+1).length());
                        {
                            finalString=s.substring(start,j+1);
                        }
                        j=start;
                        start=-1;                       
                     }
                 }
             }
    
    0 讨论(0)
提交回复
热议问题