zoukankan      html  css  js  c++  java
  • leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)

    https://leetcode.com/problems/substring-with-concatenation-of-all-words/

    You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

    For example, given:
    s: "barfoothefoobarman"
    words: ["foo", "bar"]

    You should return the indices: [0,9].
    (order does not matter).

    class Solution {
    public:
        bool func(string s, vector<string>& words, map<string, int>& h) {
            int n = words.size();
            int each = words[0].length();
            
            map<string, int> mh; mh.clear();
            int i = 0;
            for(; i<=s.length()-each; i+=each) {
                string sub = s.substr(i, each);
                
                if(h.find(sub) != h.end()) {
                    ++mh[sub];
                    if(mh[sub] > h[sub]) return false;
                }
                else return false;
            }
            
            return true;
        }
        
        vector<int> findSubstring(string s, vector<string>& words) {
            vector<int> res;
            int n = words.size();
            if(n == 0)  return res;
            
            map<string, int> h;
            for(int i=0; i<n; ++i) {
                if(h.find(words[i]) == h.end()) {
                    h.insert(make_pair(words[i], 1));
                }
                else {
                    h[words[i]]++;
                }
            }
            int each = words[0].length();
            int tot = each * n;
            if(s.length() < tot) return res;
            
            for(int i = 0; i <= s.length() - tot; ++i) {
                string sub = s.substr(i, tot);
                //cout << sub << endl;
                if(func(sub, words, h))  res.push_back(i);
            }
            
            return res;
        }
    };
    View Code

    https://leetcode.com/problems/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 empty string "".

    If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

    class Solution {
    public:
        string minWindow(string s, string t) {
            if(s.length() == 0 || s.length() < t.length())  return "";
            if(s.length() == t.length()) {
                if(s == t)  return s;
            }
            
            map<char, int> h; h.clear();
            map<char, bool> mh; mh.clear();
            for(int i=0; i<t.length(); ++i) {
                h[t[i]]++;
                mh[t[i]] = true;
            }
            
            int cnt = t.length(), l = 0, minRange = INT_MAX, mini, minj;
            for(int r=0; r<s.length(); ++r) {
                if(mh[s[r]]) {
                    --h[s[r]];
                    if(h[s[r]] >= 0)  --cnt;
                }
                
                if(cnt == 0) {
                    while(!mh[s[l]] || h[s[l]] < 0) {
                        ++h[s[l]];
                        ++l;
                    }
                    
                    if(minRange > r - l + 1) {
                        minRange = r - l + 1;
                        mini = l;
                    }
                }
            }
    
            if(minRange == INT_MAX)  return "";
            
            return s.substr(mini, minRange);
        }
    };
    View Code
  • 相关阅读:
    [script]判定dd是否成功
    [script]判定某一个脚本是否正确执行
    [wifi]wifi模块的测试
    [其他]设计开发
    [应用]Linux下" >/dev/null 2>&1 "
    [log]利用logrotate对Linux log进行管理
    [sz,rz]使用sz/rz在两台Linux设备之间传输数据
    [Kernel]内核版本添加字符和内核版本'+'解决
    [Linux应用]Linux应用程序输出数据重定向到文件中
    [systemd]How To Use Systemctl to Manage Systemd Services and Units
  • 原文地址:https://www.cnblogs.com/fu11211129/p/5215657.html
Copyright © 2011-2022 走看看