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 ...
随机推荐
- spring-如何将spring源码成功导入Eclipse中
一.从 github上下载Spring源码到本机 二.利用 Gradle 编译 Spring 源码 环境: - Spring源码版本:spring-framework-4.3.x - Gradle版本 ...
- Java8-函数复合用法
JDK8自带的函数式接口Function有两个默认方法andThen和compose,它们都返回Function的一个实例,可以用这两个方法把Function接口所代表的的Lambda表达式复合起来. ...
- Microsoft Azure Storage架构分析
Microsoft云存储服务分为两个部分,SQL Azure和Azure Storage.云存储系统的可扩展性和功能不可兼得,必须牺牲一定的关系数据库功能换取可扩展性.Microsoft实现云存储的思 ...
- iOS 8.0 bluetooth peripheral manager giving no callback for addService
I am adding the service using: [self.peripheralManager addService:myService]; Is this method depreca ...
- c# winform as3相互调用
C#主要代码: 首先要添加COM组件-Shockwave Flash Object //接收flash发送过来的信息 private void axShockwaveFlash1_Fla ...
- mac mysql5.5升级5.7记录
先删除当前的mysql文件 打开终端窗口 使用mysqldump备份你的数据库将文本文件! 停止数据库服务器 sudo rm /usr/local/mysql sudo rm -rf /usr/loc ...
- 【转】深入理解Java中的final关键字
Java 中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了 什么?使用final的好处是什么?最后也有一 ...
- [Java初探07]__关于面向对象的简单认识
前言 类和对象,在我们学习Java语言的过程中,它们无时无刻不存在着,在我们还远未详细弄明白它们的意义之前,在我们不知不觉的下意识里,我们都会直接或间接的用到它们,这是因为Java是一门面向对象的语言 ...
- 第三方登录:微信扫码登录(OAuth2.0)
1.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提供 ...
- 【杂谈】Java I/O的底层实现
前言 Java I/O功能封装的很好,使用起来很方便,就是刚开始学的时候,如果不了解装饰器模式,会被他繁多的类给吓到.用多了也就习惯了,而且现在有很多实用的封装良好的实用类,可直接读写整个文件.开发者 ...