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 ...
随机推荐
- This Gradle plugin requires Studio 3.0 minimum
从github上下载的项目遇到一个问题:Error:This Gradle plugin requires Studio 3.0 minimum 意思就是说studio版本不高,导入的项目的版本是3. ...
- [Java初探08]__简单学习Java类和对象
前言 在前面的学习中,我们对面向对象的编程思想有了一个基本的了解,并且简单的了解了类和对象的定义.那么类和对象在Java语言中是如何表现的,这次,就从实际出发,学习一下一下类和对象在Java语言中的使 ...
- linux wheel组
wheel 组的概念 wheel 组的概念继承自 UNIX.当服务器需要进行一些日常系统管理员无法执行的高级维护时,往往就要用到 root 权限:而“wheel” 组就是一个包含这些特殊权限的用户池: ...
- Linux QT 连接 Sqlite数据库
#include <QApplication> #include <QDebug> #include <QSqlQuery> #include <QSqlDa ...
- git命令小记
1.git之tag git标签分为轻量级(lightweight)标签和含标注(annotated)标签.轻量级标签一般用于不会改变的分支,含标注的标签包含详细的信息. 轻量级标签: git tag ...
- mongodb之oplog
1.查看master上当前的oplog状态: >rs.printReplicationInfo() configured oplog size: 5000MBlog length start t ...
- Linq 处理 List数据
概述:LINQ又称为语言集成查询,是一种类似于SQL的一种查询语言.语言集成查询让开发人员可以使用.NET程序语言(如C#)去查询数据源,主要数据源为内存中的集合对象.ADO.NET数据集.数据库以及 ...
- 【NOI2000】 单词查找树
问题描述 在进行文法分析的时候,通常需要检测一个单词是否在我们的单词列表里.为了提高查找和定位的速度,通常都画出与单词列表所对应的单词查找树,其特点如下: 根结点不包含字母,除根结点外每一个结点都仅包 ...
- [日常] Go语言圣经--作用域,基础数据类型,整型
go语言圣经-作用域 1.一个声明语句将程序中的实体和一个名字关联,比如一个函数或一个变量 2.一个变量的生命周期是指程序运行时变量存在的有效时间段;声明语句的作用域对应的是一个源代码的文本区域,它是 ...
- 编程输出杨辉三角的前10行---多维数组的应用---java实现
import java.util.Scanner;public class yanghui{ public static void main(String[] args){ Scanner sc=n ...