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 ...
随机推荐
- Memcache for Windows
要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); memcached-1.2.6 下载地址 环境下载 什么是MemCache memcache是一套分布式的高速缓存系统 ...
- p132代码解析
1. long before = System.currentTimeMillis(); long after = System.currentTimeMillis(); //该两句代码规定了一个do ...
- WPF中路由事件的传播
路由事件(RoutedEvent)是WPF中新增的事件,使用起来与传统的事件差别不大, 但传播方式是完全不同的. 路由事件的传播方式 通过RoutingStrategy来定义传播的方式 public ...
- 查看Linux 版本
如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux v ...
- golang三方包应该如何安装--在线和离线
一 在线安装 采用go get的方式安装import 的时候找不到对应的包看看pkg里面有没有 二 离线安装 redis客户端采用git clone的方法安装的话可以用以下方法 cd src git ...
- ELK之filebeat
1.概述 filebeat使用go语言开发,轻量级.高效.主要由两个组件构成:prospector和harvesters. Harvesters负责进行单个文件的内容收集,在运行过程中,每一个Harv ...
- PHP filter_input() 函数
以往,对于常见的SQL注入等漏洞,采取的方式一般都是对数据进行过滤,而对$_GET/$_POST/$_COOKIE/$_SERVER等全局数组变量的直接使用是不够安全的,故PHP 5.2.0版本以后, ...
- aspnetmvc和aspnetcoremvc的一些区别
1.路由 控制器添加特性: [RoutePrefix("api/controller")] = > [Route("api/[controller]&quo ...
- SQL Server ——用 join on 连接多个表
select * from table1 inner join table2 on table1.id=table2.id 其实 INNER JOIN ……ON的语法格式可以概括为: FROM ((( ...
- Shiro眼皮下玩ajax,玩出302 Found
2017/06/14这一天,是我玩Shiro安全框架最刻骨铭心的一天.因为Shiro今天给我深深的补了一刀,在这儿我也给各位补一刀吧,其实问题很简单,解决方式也极其简单,只是给各位分享一下这个错误,纯 ...