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

    Analyse: Look at the code below. 
    Runtime: 1548ms. 
     1 class Solution {
     2 public:
     3     //first scan the words, and put each word and its occurance time into an map
     4     //then gradually scan the the string, create a new map and compare it with original map
     5     //if equal, push the first index into result vector
     6     vector<int> findSubstring(string s, vector<string>& words) {
     7         vector<int> result;
     8         if(s.empty() || words.empty()) return result;
     9         int wordsNumber = words.size();
    10         int wordLen = words[0].size();
    11         int allWordsLen = wordsNumber * wordLen;
    12         if(s.size() < allWordsLen) return result;
    13         
    14         unordered_map<string, int> um;
    15         for(int i = 0; i < words.size(); i++)
    16             um[words[i]]++;
    17             
    18         for(int i = 0; i < s.size() - allWordsLen + 1; i++){
    19             unordered_map<string, int> tempUm;
    20             for(int j = 0; j < wordsNumber; j++){
    21                 string temp = s.substr(i + j * wordLen, wordLen);
    22                 tempUm[temp]++;
    23             }
    24             if(tempUm == um)
    25                 result.push_back(i);
    26         }
    27         return result;
    28     }
    29 };

    After adding a judgement, the run time has been reduced to 596ms.

     1 class Solution {
     2 public:
     3     //first scan the words, and put each word and its occurance time into an map
     4     //then gradually scan the the string, create a new map and compare it with original map
     5     //if equal, push the first index into result vector
     6     vector<int> findSubstring(string s, vector<string>& words) {
     7         vector<int> result;
     8         if(s.empty() || words.empty()) return result;
     9         int wordsNumber = words.size();
    10         int wordLen = words[0].size();
    11         int allWordsLen = wordsNumber * wordLen;
    12         if(s.size() < allWordsLen) return result;
    13         
    14         unordered_map<string, int> um;
    15         for(int i = 0; i < words.size(); i++)
    16             um[words[i]]++;
    17             
    18         for(int i = 0; i < s.size() - allWordsLen + 1; i++){
    19             //find the first matched word
    20             bool found = false;
    21             for(int k = 0; k < wordsNumber; k++){
    22                 if(s.substr(i, wordLen) == words[k]){
    23                     found = true;
    24                     break;
    25                 }
    26             }
    27             if(!found) continue;
    28             
    29             unordered_map<string, int> tempUm;
    30             for(int j = 0; j < wordsNumber; j++){
    31                 string temp = s.substr(i + j * wordLen, wordLen);
    32                 tempUm[temp]++;
    33             }
    34             if(tempUm == um)
    35                 result.push_back(i);
    36         }
    37         return result;
    38     }
    39 };
  • 相关阅读:
    LotusAdmin帮开发者节省两天开发时间,基于thinkphp6开发的rbac admin(原创)
    Linux自动化分区脚本
    解决github图片无法加载_加快github访问速度
    怎么彻底关闭flash助手弹窗?
    匹配得到A标签href的内容 亲测有效赶紧收藏
    不要再安装vmware-tools了,在 VMware 虚拟机中安装 open-vm-tools可以代替了
    使用root新建管理员用户
    关于我的博客
    C++静态全局变量问题
    SVN 提交子文件夹问题
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5208673.html
Copyright © 2020-2023  润新知