博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】30. Substring with Concatenation of All Words 精确匹配的子串集
阅读量:7250 次
发布时间:2019-06-29

本文共 2796 字,大约阅读时间需要 9 分钟。

1. 题目

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.

For example, given:

s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9].

(order does not matter).

2. 思路

没有特别的好方法,相当于死扛计算的。

注意:words里可能有重复。
注意:“ab”,“ba”, "aa", "bb"这种,彼此交叉的。

遍历起点,每次找到一个匹配点,然后验证是否符合需求。然后+1从下一个点开始找下一个起点。

起点当前的点的第一个串是其中一个。
验证方法是,看是否能匹配到words.size个。特殊处理words里重复的情况,通过计数处理。
为每一个起点开始的匹配,为word命名一个version,记录下version。重复出现version时进行count比较。count超了就失败。

3. 代码

耗时:849ms

class Solution {public:    vector
findSubstring(string& s, vector
& words) { //cout << "s=" << s << endl; vector
ret; if (words.size() == 0) { return ret; } unordered_map
map; unordered_map
freq; for (int i = 0; i < words.size(); i++) { map[words[i]] = 0; if (freq.find(words[i]) != freq.end()) { freq[words[i]]++; } else { freq[words[i]] = 1; } } int version = 0; size_t i = 0; size_t one_len = words[0].length(); size_t num = words.size(); size_t total_len = one_len * num; while (i < s.length() - total_len + 1) { i = findFirstOf(s, i, words); if (i == string::npos) { return ret; } string s1 = s.substr(i, one_len); version++; unordered_map
cnt; cnt[s1] = 1; //cout << "i=" << i << " version=" << version << " s1=" << s1 << endl; int count = 0; int k = i; map[s1] = version; //cout << "count=" << count << " k=" << k << " str=" << s1 << " it=" << version << endl; k += one_len; count++; while (count < num) { string s1 = s.substr(k, one_len); auto it = map.find(s1); //cout << "count=" << count << " k=" << k << " str=" << s1 << " it=" << (it == map.end() ? -1 : it->second) << endl; if (it == map.end()) { break; } if (it->second == version) { cnt[s1]++; if (cnt[s1] > freq[s1]) { break; } } else { cnt[s1] = 1; } map[s1] = version; k += one_len; count++; } if (count == num) { ret.push_back(i); //cout << "find idx: " << i << endl; } i++; } return ret; } size_t findFirstOf(string&s, size_t b, vector
& words) { size_t min = s.length(); for (int i = 0; i < words.size(); i++) { size_t cur = s.find(words[i], b); //cout << "firstOf: " << words[i] << " cur=" << cur << " s=" << s.substr(b) << endl; if (cur == string::npos) { return string::npos; } if (cur < min) { min = cur; } } if (min == s.length()) { return string::npos; } else { return min; } }};

转载地址:http://fehbm.baihongyu.com/

你可能感兴趣的文章
用flutter写一个精美的登录页面
查看>>
[转]Docker php extensions gd
查看>>
Java Program Mapping GB2312 to Unicode
查看>>
C语言标准中的逻辑位移和算术位移
查看>>
查看当前运行的SQL语句
查看>>
【Python】opencv显示图像
查看>>
Web配置文件(web.config)简介
查看>>
如何培养员工的团队合作精神
查看>>
POJ 1151 Atlantis (线段树)
查看>>
在sqlserver中如何根据字段名查找字段所在的表
查看>>
quality center 11备份最佳方案测试通过可用
查看>>
一本比较简单易懂的中文python入门教程
查看>>
CDN和双线机房相比有何优势
查看>>
soapui not supported the auto complete
查看>>
Tomcat配置并启用HTTPS
查看>>
javascript调用WebService - Hello World
查看>>
【Tomcat】Servlet 工作原理解析
查看>>
C#设计模式(19)——状态者模式(State Pattern)
查看>>
UVA 10173 Smallest Bounding Rectangle(最小外接矩形)
查看>>
Top 126 Ajax Tutorials
查看>>