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

后端 未结 15 1079
灰色年华
灰色年华 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

    I received the same interview question. I am a C++ candidate but I was in a position to code relatively fast in JAVA.

    Java [Courtesy : Sumod Mathilakath]

    import java.io.*;
    import  java.util.*;
    
    class UserMainCode
    {
    
    
        public String GetSubString(String input1,String input2){
            // Write code here...
            return find(input1, input2);
        }
      private static boolean containsPatternChar(int[] sCount, int[] pCount) {
            for(int i=0;i<256;i++) {
                if(pCount[i]>sCount[i])
                    return false;
            }
            return true;
        }
      public static String find(String s, String p) {
            if (p.length() > s.length())
                return null;
            int[] pCount = new int[256];
            int[] sCount = new int[256];
            // Time: O(p.lenght)
            for(int i=0;i

    C++ [Courtesy : sundeepblue]

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    string find_minimum_window(string s, string t) {
        if(s.empty() || t.empty()) return;
    
        int ns = s.size(), nt = t.size();
        vector total(256, 0);
        vector sofar(256, 0);
        for(int i=0; i total[c]) {
                        sofar[c]--;
                        L++;
                    }
                    else break;
                }  
                if(R - L + 1 < min_win_len) {   // this judge should be inside POS1
                    min_win_len = R - L + 1;
                    minL = L;
                }
            }
        }
        string res;
        if(count == nt)                         // gist3, cannot forget this. 
            res = s.substr(minL, min_win_len);  // gist4, start from "minL" not "L"
        return res;
    }
    int main() {
        string s = "abdccdedca";
        cout << find_minimum_window(s, "acd");
    }
    

    Erlang [Courtesy : wardbekker]

    -module(leetcode).
    
    -export([min_window/0]).
    
    %% Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
    
    %% For example,
    %% S = "ADOBECODEBANC"
    %% T = "ABC"
    %% Minimum window is "BANC".
    
    %% Note:
    %% If there is no such window in S that covers all characters in T, return the emtpy string "".
    %% If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
    
    
    
    min_window() ->
        "eca" = min_window("cabeca", "cae"),
        "eca" = min_window("cfabeca", "cae"),
        "aec" = min_window("cabefgecdaecf", "cae"),
        "cwae" = min_window("cabwefgewcwaefcf", "cae"),
        "BANC" = min_window("ADOBECODEBANC", "ABC"),
        ok.
    
    min_window(T, S) ->
        min_window(T, S, []).
    
    min_window([], _T, MinWindow) ->
        MinWindow;
    min_window([H | Rest], T, MinWindow) ->
        NewMinWindow = case lists:member(H, T) of
                           true ->
                               MinWindowFound = fullfill_window(Rest, lists:delete(H, T), [H]),
                               case length(MinWindow) == 0 orelse (length(MinWindow) > length(MinWindowFound)
                                   andalso length(MinWindowFound) > 0) of
                                   true ->
                                       MinWindowFound;
                                   false ->
                                       MinWindow
                               end;
                           false ->
                               MinWindow
                       end,
        min_window(Rest, T, NewMinWindow).
    
    fullfill_window(_, [], Acc) ->
        %% window completed
        Acc;
    fullfill_window([], _T, _Acc) ->
        %% no window found
        "";
    fullfill_window([H | Rest], T, Acc) ->
        %% completing window
        case lists:member(H, T) of
            true ->
                fullfill_window(Rest, lists:delete(H, T), Acc ++ [H]);
            false ->
                fullfill_window(Rest, T, Acc ++ [H])
        end.
    

    REF:

    • http://articles.leetcode.com/finding-minimum-window-in-s-which/#comment-511216
    • http://www.mif.vu.lt/~valdas/ALGORITMAI/LITERATURA/Cormen/Cormen.pdf

提交回复
热议问题