Minimum Window Substring--Leetcode

Source: Internet
Author: User

Topic:

Given a string S and a string T, find the minimum window in S which would contain all the characters in T in complexity O (n ).

for example,
s  = " Adobecodebanc "
t  = < Code style= "Font-family:monaco,menlo,consolas, ' Courier New ', monospace; font-size:13px; PADDING:2PX 4px; Color:rgb (199,37,78); White-space:nowrap; Background-color:rgb (249,242,244) ">" ABC "

Minimum window is "BANC" .

Note:
If There is no such window in S so covers all characters in T, return the emtpy string "" .

If There is multiple such windows, you is guaranteed that there would always being only one unique minimum window in S.

Idea: Use two pointers, a hash table, two pointers in order to record the width, the hash table in order to record the existence of the target character in the two pointers appear between the number of times, until all the characters appear in the string, moving the preceding pointer, until a character appears in the substring once, So the length of the space is the shortest, so the traversal is done until all the strings are traversed.

#include <iostream> #include <vector> #include <string>using namespace std;/* A string can contain the minimum length of substrings of all letters in another string */String MinLength (string& src,string& dest) {int I=0,j=0;int flag =0;int len= Src.size (); int pos=0;vector<int> hash (26,-1); for (I=0;i<dest.length (); i++) hash[dest[i]-' A '] =0;//for (i=0; I

Minimum Window Substring--Leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.