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 ...
随机推荐
- ActiveMQ配置高可用性的方式
当一个应用被部署于生产环境,灾备计划是非常重要的,以便从网络故障,硬件故障,软件故障或者电源故障中恢复.通过合理的配置ActiveMQ,可以解决上诉问题.最典型的配置方法是运行多个Broker,一旦某 ...
- [转]让程序不触发 Vista/Win7下应用程序兼容性助手弹出 .
原文地址 http://blog.csdn.net/maxuhuiabc/article/details/6081874 在Vista/Win7下 运行一个 exe 应用程序后,系统经常弹出 兼容性助 ...
- VMWare Workstation 11的安装
不多说,直接上干货! 说白了 VMWare Workstation是收费的! VMware Player 和 VirtualBox是免费的! 虚拟机软件可让你在一个操作系统上直接运行的多个不同的虚 ...
- php -- 日期时间
----- 017-datetime.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&qu ...
- Hadoop2源码分析-序列化篇
1.概述 上一篇我们了解了MapReduce的相关流程,包含MapReduce V2的重构思路,新的设计架构,与MapReduce V1的区别等内容,今天我们在来学习下在Hadoop V2中的序列化的 ...
- Charles抓包之HTTPS抓包配置
访问我的博客 前言 由于工作中经常需要配置客户端开发人员对接接口,有时候对接地不太顺利,因此需要经常性地对公司 APP 进行抓包看请求,找出具体的原因. 在公司中开发使用的 Windows 台式电脑, ...
- Tomcat学习总结(7)——Tomcat与Jetty比较
Jetty 基本架构 Jetty目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的应用服务器. 它有一个基本数据模型,这个数据模型就是 Handler(处理 ...
- window如何一键关闭所有进程程序
1.桌面创建一个快捷方式,点击鼠标右键跳出的菜单=> 新建=>快捷方式 2. 在“请键入对象的位置(T)”下方的文本框中输入: taskkill /F /FI "USERNAME ...
- Docker实战-为镜像添加SSH服务
1.基于docker commit命令创建 Docker提供了docker commit命令,支持用户提交自己对定制容器的修改,并生成新的镜像. 命令格式为:docker commit CONTAIN ...
- Spring Security基本配置
Spring Security 是一个功能强大且可高度自定义的身份验证和访问控制框架. 它是保护基于Spring的应用程序的事实上的标准.Spring Security 是一个专注于为Java应用程序 ...