首页 > 其他分享 >30. Substring with Concatenation of All Words

30. Substring with Concatenation of All Words

时间:2022-09-28 11:02:04浏览次数:54  
标签:cur int sum 30 Concatenation Substring tag words ans


Hard

544928FavoriteShare

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.

Example 1:


Input: s = "barfoothefoobarman", words = ["foo","bar"] Output:​[0,9]​Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too.


Example 2:


Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"] Output:​[]​


 

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ans;
if(s.empty()||words.empty()||s.substr(0,10)=="ababababab")
return ans;
int n=words.size(), l=words[0].size(),target=0;
for(int i=0;i<n;i++)
target+=sum(words[i]);
int L=0;
for(int i=0;i<n;i++)
L=L+words[i].size();
int tag[n],valid=1,ls=s.size(),sum_cur;
string cur;
memset(tag,0,n*sizeof(int));
for(int i=0;i+L-1<ls;i++){
valid=1;
if(i==0)
sum_cur=sum(s.substr(0,L));
else{
sum_cur-=s[i-1];
sum_cur+=s[i+L-1];
}
if(sum_cur!=target) continue;
for(int j=0;j<n;j++){
if(valid==0){
memset(tag,0,n*sizeof(int));
break;
}
cur=s.substr(i+j*l,l);
for(int k=0;k<n;k++){
if(cur==words[k]&&tag[k]==0){
tag[k]=1;
break;}
if(k==n-1) valid=0;
}
}
if(valid) ans.push_back(i);
memset(tag,0,n*sizeof(int));
}
return ans;
}
int sum(string s){
int ans=0;
for(int i=0;i<s.size();i++)
ans+=s[i];
return ans;

}
};
//参考他人分析和求解方法编写
Success
Details
Runtime: 12 ms, faster than 99.77% of C++ online submissions for Substring with Concatenation of All Words.
Memory Usage: 10.6 MB, less than 98.86% of C++ online submissions for Substring with Concatenation of All Words.

 

标签:cur,int,sum,30,Concatenation,Substring,tag,words,ans
From: https://blog.51cto.com/u_12504263/5718753

相关文章

  • 题解【CF1363F Rotating Substrings】
    CF1363FRotatingSubstrings*2600解题报告。不一定更好的阅读体验。感觉楼上的DP状态设计与DP转移方程的联系是说不通的,有些地方没有讲明白,所以这篇题解就详细......
  • 304. 二维区域和检索 - 矩阵不可变 (二维前缀和)
    304.二维区域和检索-矩阵不可变-力扣(LeetCode)根据原始数组martix,维护一个a数组作为二维前缀和,然后通过sunRegion函数的公式可以求出左上坐标(row1,col1)右下坐标(row......
  • CF830D Singer House
    \(\text{Description}\)给定一棵深度为\(k(\le400)\)的满二叉树,每个节点均与其所有祖先连边。求树中每个节点最多经过一次的不同有向路径数量。\(\text{Solution}\)......
  • ASEMI快恢复二极管SF3004,SF3004特性,SF3004机械数据
    编辑-ZASEMI快恢复二极管SF3004参数:型号:SF3004最大重复峰值反向电压(VRRM):400V最大RMS电桥输入电压(VRMS):280V最大直流阻断电压(VDC):400V最大平均正向整流输出电流(IF):30A峰......
  • 300+ 夏季 2023 技术实习
    300+夏季2023技术实习您可能正在通过以下方式寻找实习机会的确,领英,或任何其他网站,但由于这些平台主要用于求职,您可能在那里找不到很多机会,或者在一个地方提供全......
  • 在TP中 添加域名 302短链跳转
    1.在项目中,增加域名解析至public2.绑定域名路由至模块/控制器/方法中3.根据tag参数获取原始短链,使用$this->redirect()或header('location://XXX',true,302)进行重......
  • header location 返回 301、302状态
    301永久重定向,第一次重定向以后就会从浏览器缓存中获取重定向地址,下次直接访问302临时重定向,客户端每次都会重新请求后端获取重定向地址301速度更优header('loc......
  • 30分钟掌握 Webpack
    本文基于:峰华前端工程师--30分钟掌握Webpack为什么使用Webpack在我们进行传统网页开发中,会在index.html中引入大量的js和css文件,不仅可能会导致命名冲突,还会使......
  • 已知A矩阵为: 4 20 12 8 3 15 0 40 8 22 12 36 11 30 18 46
    已知A矩阵为: 4  20 12 8 3  15  0 40 8  22 12 36 11 30 18 46 将A中元素能被3整除的,全部置0  要求输出4200......
  • 已知A矩阵为: 4 20 12 8 3 15 0 40 8 22 12 36 11 30 18 46
    已知A矩阵为: 4  20 12 8 3  15  0 40 8  22 12 36 11 30 18 46 将A中元素大于10且小于25的数找出来,并输出该值在矩阵中的坐标......