[LeetCode]76.Minimum Window Substring

题目

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.

分析

详细些参考:[算法系列之二十二]包含T全部元素的最小子窗口

代码

/*--------------------------------------------
*   日期:2015-02-24
*   作者:SJF0115
*   题目: 76.Minimum Window Substring
*   网址:https://oj.leetcode.com/problems/minimum-window-substring/
*   结果:AC
*   来源:LeetCode
*   总结:
------------------------------------------------*/
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;

class Solution {
public:
    string minWindow(string S, string T) {
        int slen = S.size();
        int tlen = T.size();
        if(slen <= 0 || tlen <= 0){
            return "";
        }//if
        int minWinStart = 0,minWinEnd = 0;
        int minWinLen = INT_MAX;
        // 存储到目前为止遇到过的T中字符总数
        int count = 0;
        // 存储T中不同字符的总数
        int needFind[256] = {0};
        for(int i = 0;i < tlen;++i){
            ++needFind[T[i]];
        }//for
        // 存储到目前为止遇到过的不同字符的总数
        int hasFound[256] = {0};
        int val;
        for(int start = 0,end = 0;end < slen;++end){
            val = S[end];
            // 跳过不在T中的字符
            if(needFind[val] == 0){
                continue;
            }//if
            ++hasFound[val];
            if(hasFound[val] <= needFind[val]){
                ++count;
            }//if
            // 找到一个有效窗口
            if(count == tlen){
                int startVal = S[start];
                while(needFind[startVal] == 0 ||
                      hasFound[startVal] > needFind[startVal]){
                    if(hasFound[startVal] > needFind[startVal]){
                        --hasFound[startVal];
                    }//if
                    ++start;
                    startVal = S[start];
                }//while
                // 更新最小窗口
                int curWinLen = end - start + 1;
                if(curWinLen < minWinLen){
                    minWinLen = curWinLen;
                    minWinStart = start;
                    minWinEnd = end;
                }//if
            }//if
        }//for
        if(count != tlen){
            return "";
        }//if
        return S.substr(minWinStart,minWinEnd - minWinStart + 1);
    }
};

int main() {
    Solution solution;
    string S("acbbaca");
    string T("aba");
    cout<<solution.minWindow(S,T)<<endl;
}

运行时间

相似题目:

[经典面试题][搜狗]在一个字符串中寻找包含全部出现字符的最小字串

时间: 2024-10-30 14:10:34

[LeetCode]76.Minimum Window Substring的相关文章

Minimum Window Substring

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 i

[LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

[LeetCode]239.Sliding Window Maximum

题目 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. For example, Given

[LeetCode]111.Minimum Depth of Binary Tree

[题目] Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. [分析] 类似于:LeetCode之Maximum Depth of Binary Tree [代码] /********************************

[LeetCode]64.Minimum Path Sum

[题目] Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. [思路] 设sum[i][j] 到位置(i,j)时路径最

[LeetCode]5.Longest Palindromic Substring

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路一 即不使用技巧,穷举所有可能.时间复杂度为O(n^3)(时间上最长,不推荐使用),空间复杂度为O(1).本思路是从最大长度的字串开始,而不

[LeetCode] Find Minimum in Rotated Sorted Array II

Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might b

LeetCode 5 Longest Palindromic Substring(最大回文子字符串)

翻译 给定一个字符串S,找出它的最大回文子字符串. 你可以假定S的最大长度为1000, 并且这里存在唯一一个最大回文子字符串. 原文 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 暴力搜索,O(n

[经典面试题][搜狗]在一个字符串中寻找包含全部出现字符的最小字串

题目 一个字符串中含有n个字符,其中有m个不同的字符,n>>m,用最少的时间和空间找到包含所有这m个字符的最短的字串,不考虑特殊字符,只考虑字母数字即可. 例如: abccbaddac, 返回:cbad aabcadbbbcca,返回:bcad 思路 [算法系列之二十二]包含T全部元素的最小子窗口 本题目相比连接中所说的稍微简单一些,本题目不用考虑重复字符. 代码 /*--------------------------------------------- * 日期:2015-02-24 *