leetcode 之 Substring with Concatenation of All Words,leetcode

來源:互聯網
上載者:User

leetcode 之 Substring with Concatenation of All Words,leetcode

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).

思路:題目的意思是找到包含L中所有單詞的起始位置,但是不能多,也不能少,要剛好包含L中的全部單詞,而且相鄰的兩個開始位置還可以重疊,比如:S="aaa",L=['a','a'],結果是[0,1]。具體方法和最短摘要有點類似,用need數組儲存需要的所有單詞,hasFind儲存已經找到的單詞,如果hasFind大於need,則該位置不滿足,只有當兩個數組完全相同時才滿足。

class Solution {public:    vector<int> findSubstring(string S, vector<string> &L)    {    int totalWord = L.size(),wordLen = L[0].size(),i,j;    map<string,int> needWord;    vector<int> res;    for (i = 0;i < totalWord;i++)needWord[L[i]]++;//統計需要的單詞    for(i = 0;i + totalWord*wordLen <= S.size();i++)    {    map<string,int> hasFindWord;    for (j =0;j < totalWord;j++)//對於每一個單詞    {    string sub = S.substr(i+j*wordLen,wordLen);//提取每一個可能的單詞    map<string,int>::iterator iter = needWord.find(sub);    if(iter == needWord.end())break;    hasFindWord[sub]++;    if(hasFindWord[sub] > needWord[sub])break;    }    if(j == totalWord)res.push_back(i);    }    return res;    }};



階梯英語的POPODOO美語好不好?與新東方英語比,哪個更適合7歲的孩子學?有沒有便宜的教材?

新東方主要是要找到好老師。服務比較完善,興趣激發也很好,價錢比較合理。
 
新概念英語第三冊摘要寫作全部 網址也行 或發郵件至545174359@qqcom 謝了

到書店買本教師用 新概念 上面應該都有的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.