LeetCoder题解之Find All Anagrams in a String
1、题目描述

2、题目分析
直接使用 哈希表记录子串的信息,然后对比两个哈希表信息即可
3、代码
vector<int> findAnagrams(string s, string p) {
vector<int> b;
if( s.size() < p.size() )
return b;
vector<int> ans;
map<char,int> m;
for(auto c: p){
m[c]++;
}
map<char,int> sm;
for(int i = ; i < s.size() - p.size()+; ++i){
string sb = s.substr(i,p.size());
if( i == ){
for( auto c : sb){
sm[c]++;
}
}else{
if( sm[s[i-]] <= ){
sm.erase(s[i-]);
}else{
sm[s[i-]]--;
}
sm[s[i+p.size()-]]++;
}
bool m1 = true , m2 = true ;
for(map<char,int>::iterator it = sm.begin(); it != sm.end() ; it++){
if( m.find(it->first) == m.end() || m[ it->first] != it->second ){
m1 = false;
break;
}
}
if( m1 == true ){
for(map<char,int>::iterator it = m.begin() ; it != m.end() ; ++it){
if( sm.find(it->first) == sm.end() || sm[it->first] != it->second){
m2 = false;
break;
}
}
}
if( m1 == true && m2 == true)
ans.push_back(i);
}
return ans;
}
LeetCoder题解之Find All Anagrams in a String的更多相关文章
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- 【题解】CF1290B Irreducible Anagrams
Link 题目大意:对于一个字符串,每次询问一个区间,看看这个区间是不是可以划分为若干区间,这些区间内数字经过排列后可以还原原来区间. \(\text{Solution:}\) 菜鸡笔者字符串构造该好 ...
- LeetCode Find All Anagrams in a String
原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- [leetcode-438-Find All Anagrams in a String]
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.Strings c ...
- [Swift]LeetCode438. 找到字符串中所有字母异位词 | Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
随机推荐
- Selenium自动化测试Python一:Selenium入门
Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介绍Selenium的入门知识以及Selenium的前置知识. 自动化测试的基础 在Selenium的课程以前,我们先回顾一下软件 ...
- Android_view的生命周期
onFinishInflate() 当View中所有的子控件均被映射成xml后触发 onMeasure( int , int ) 确定所有子元素的大小 onLayout( boolean , int ...
- JDK7 异常的多重捕获
Java 7之前的版本: 1 2 3 4 5 6 7 8 9 10 catch (IOException ex) { logger.error(ex); throw new MyE ...
- ActiveMQ——activemq的报错见解javax.jms.JMSException: Software caused connection abort: recv failed
activeMQ出现javax.jms.JMSException: Software caused connection abort: recv failed的问题解决 一直找不到原因,原来是在本地的 ...
- git 分支 branch 操作
创建分支 git branch test: 基于当前commit创建test分支..git/HEAD 文件中记录了当前分支名字. 删除分支 git branch -d test:删除本地test分支 ...
- Charm Bracelet(01背包问题)
题目链接: https://vjudge.net/problem/POJ-3624 题目描述: Bessie has gone to the mall's jewelry store and spie ...
- mybatis框架下物理分页的实现(整个工程采用的是springmvc、spring、mybatis框架,数据库是mysql数据库)
(一)关于分页拦截器的简单理解 首先,要开发MyBatis的插件需要实现org.apache.ibatis.plugin.Interceptor接口,这个接口将会要求实现几个方法:intercept( ...
- Java 并发编程常识 —— by 梁飞
参考 :梁飞 并发编程常识
- Spark入门——什么是Hadoop,为什么是Spark?
#Spark入门#这个系列课程,是综合于我从2017年3月分到今年7月份为止学习并使用Spark的使用心得感悟,暂定于每周更新,以后可能会上传讲课视频和PPT,目前先在博客园把稿子打好.注意:这只是一 ...
- [转]论magento1和magento2的速度性能优化问题
本文转自:http://www.360magento.com/blog/magento-speed-up/ magento从2007年发展至今,也经历了十余年的磨练,如今也迎来了magento的换代产 ...