博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 438. Find All Anagrams in a String
阅读量:5919 次
发布时间:2019-06-19

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

Description

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

Example 1:Input:s: "cbaebabacd" p: "abc"Output:[0, 6]Explanation:The substring with start index = 0 is "cba", which is an anagram of "abc".The substring with start index = 6 is "bac", which is an anagram of "abc".Example 2:Input:s: "abab" p: "ab"Output:[0, 1, 2]Explanation:The substring with start index = 0 is "ab", which is an anagram of "ab".The substring with start index = 1 is "ba", which is an anagram of "ab".The substring with start index = 2 is "ab", which is an anagram of "ab".

My solution

  • 最简单的想法就是从1->psize, 2->psize+1, 3->psize+2,... 每次都计算并比较两个map是否一样
  • 考虑到每次只有一个新元素进来,一个旧元素离开,所以算法可以简化为只对变化的地方修正map
  • 又考虑到每次比较两个map全部元素也是冗余的,因为只需要比较变化的地方, 倘若某些元素已经是s<=>p匹配的了,就可以忽略,我采用的方式是建立一个待考察的map(命名为dif), 当dif为空, 则两个map无差异, res.push_back即可; 当dif不为空, 就说明滑窗需要继续前进.这样便省去了每次比较两个map的O(psize)的复杂度.
  • 总的来说基本思路是unordered_map思路, 当然代码有待优化
class Solution {public:    vector
findAnagrams(string s, string p) { vector
res; int ssize = s.size(); int psize = p.size(); unordered_map
mp; for (int i = 0; i < psize; ++i) --mp[p[i]]; unordered_map
dif = mp; for (int i = 0; i < psize; ++i) { if (++dif[s[i]] == 0) dif.erase(s[i]); } if (dif.empty()) res.push_back(0); for (int i = psize; i < ssize; ++i) { if (++dif[s[i]] == 0) dif.erase(s[i]); if (--dif[s[i - psize]] == 0) dif.erase(s[i - psize]); if (dif.empty()) res.push_back(i - psize + 1); } return res; }};

Discuss

下面代码和我的思路基本一致, 不过因为字母连续有限, 直接用26个空间的vector存储(这是特例情况下的操作). 当然vector的存取时间消耗应该是更高的, 尤其是下文代码中p==v的步骤.

class Solution {public:    vector
findAnagrams(string s, string p) { vector
pv(26,0), sv(26,0), res; if(s.size() < p.size()) return res; // fill pv, vector of counters for pattern string and sv, vector of counters for the sliding window for(int i = 0; i < p.size(); ++i) { ++pv[p[i]-'a']; ++sv[s[i]-'a']; } if(pv == sv) res.push_back(0); //here window is moving from left to right across the string. //window size is p.size(), so s.size()-p.size() moves are made for(int i = p.size(); i < s.size(); ++i) { // window extends one step to the right. counter for s[i] is incremented ++sv[s[i]-'a']; // since we added one element to the right, // one element to the left should be forgotten. //counter for s[i-p.size()] is decremented --sv[s[i-p.size()]-'a']; // if after move to the right the anagram can be composed, // add new position of window's left point to the result if(pv == sv) res.push_back(i-p.size()+1); } return res; }};

后记

为什么我觉得我的方式很优秀呢..难道是幻觉..

Reference

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

你可能感兴趣的文章
关于本人拙著《Cocos2d-x 3.x实战:卡牌手游开发指南》源码的有关说明
查看>>
BeX5开发中MySQL视图使用的一个小问题
查看>>
中小企业网络优化
查看>>
“容灾三化” 政府行业容灾一劳永逸
查看>>
Radware:应用交付向云端扩展
查看>>
我的友情链接
查看>>
SAP GUI常用快捷键
查看>>
Linux Open*** MYSQL FreeRadius 全面安装手册指导
查看>>
2013年你进步了么?技术男职业成长必经之路
查看>>
BGP as-path 属性
查看>>
PHP优化杂烩
查看>>
寒冬之后,4G报春
查看>>
sshd2配置指要
查看>>
macbook pro 700自己换SSD 开启AHC
查看>>
Excel2007给表格设置成只读加密属性 让他人无法修改
查看>>
不停机处理oracle超过最大processes数故障
查看>>
我的友情链接
查看>>
ORA-00328 ORA-00334 MRP0: Background Media Recovery terminated with error 328
查看>>
我的友情链接
查看>>
linux arguments variables usage
查看>>