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

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

    For example, given:
    S: "barfoothefoobarman"
    L: ["foo", "bar"]

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

    class Solution {
    public:
        vector<int> findSubstring(string S, vector<string> &L) {
        vector<int> res;
        int len=L[0].size()*L.size();
        if (S.empty()||L.empty()||S.size()<len)return res;
        map<string,int> Lmap;
        map<string,int> temp;
        for (int i=0;i<L.size();i++)
            Lmap[L[i]]++;
        for (int i=0;i<=(int)S.size()-len;i++)
        {
            temp.clear();
            int j=i;
            for (;j<i+len;j+=L[0].size())
            {
                string word=S.substr(j,L[0].size());
                if (Lmap.find(word)==Lmap.end())
                    break;
                temp[word]++;
                if (temp[word]>Lmap[word])
                    break;
            }
            if (j==i+len)
                res.push_back(i);
        }
        return res;
        }
    };
  • 相关阅读:
    JVM(java 虚拟机)内存设置
    elasticsearch 安装和 基本操作命令
    Spring
    springmvc 文件上传和拦截器
    InitBinder和数据校验
    异常、类型转换
    git 提交方式
    Springmvc(二)
    Springmvc(一)
    SSM
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4283617.html
Copyright © 2011-2022 走看看