zoukankan      html  css  js  c++  java
  • [leetcode]Substring with Concatenation of All Words

    用hash暴力。。。

    感觉是刚好过吧。。

    开始里面多查了两次,就是TLE

    class Solution {
    public:
        vector<int> findSubstring(string S, vector<string> &L) {
            int wordLen = L.front().size();
            int cut = L.size() * wordLen;
            int s_size = S.size();
            vector<int> ans;
            if(s_size < cut) return ans;
            unordered_map<string , int> dic;
            for(int i = 0 ; i < L.size() ; i++) dic[L[i]] ++;
            
            for(int i = 0 ; i <= s_size - cut ; i ++){
                //--
                unordered_map<string,int> tmp(dic);
                for(int j = i ; j < i + cut ; j += wordLen){
                    string sub = S.substr(j , wordLen);
                    auto found = tmp.find(sub);
                    if(found == tmp.end() || found -> second == 0) break;
                    found->second--;
                    if(found -> second == 0) tmp.erase(found);
                }
                if(tmp.size() == 0) ans.push_back(i);
                
            }
            return ans;
        }
    };
    class Solution {
    public:
        vector<int> findSubstring(string S, vector<string> &L) {
            unordered_map<string, int> cnt;
            int n = L.size();
            int len = L.front().size();
            for (auto& str : L) cnt[str]++;
            vector<int> ans;
            if (S.size() < n * len) return ans;
    
            for (int i = 0; i <= S.size() - n * len; i++) {
                unordered_map<string, int> tmp(cnt);
                for (int j = i; j < i + n * len; j += len) {
                    string sub = S.substr(j, len);
                    auto found = tmp.find(sub);
                    if (found == tmp.end() || found->second == 0) break;
                    found->second--;
                    if (found->second == 0) {
                        tmp.erase(found);
                    }
                }
                if (tmp.size() == 0) {
                    ans.push_back(i);
                }
            }
            return ans;
        }
    };
  • 相关阅读:
    如何自建appender扩展Log4j框架
    在O(1)时间删除链表结点
    My First GitHub
    JAVA序列化和反序列化
    [转]Vim 复制粘帖格式错乱问题的解决办法
    Centos清理内存 内存回收释放及内存使用查看的相关命令
    Spark HA 的搭建
    Ambari安装
    Hadoop HA的搭建
    Hadoop32位和64位的查询
  • 原文地址:https://www.cnblogs.com/x1957/p/3511084.html
Copyright © 2011-2022 走看看