【leetcode】438. Find All Anagrams in a String
problem
438. Find All Anagrams in a String
solution1:
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if(s.empty()) return {};
vector<int> res, pv(, );
for(auto a:p) pv[a]++;
int sn = s.size();
int i = ;
while(i<sn)
{
vector<int> tmp = pv;
bool is = true;
for(int j=i; j<i+p.size(); j++)
{
if(--tmp[s[j]]<)
{
is = false;
break;
}
}
if(is) res.push_back(i);
i++;
}
return res;
}
};
solution2:使用哈希表表示一定字符长度内各个字符的个数,每次滑窗需要添加最新的字符,且减去最旧的字符,然后比较哈希表。
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if(s.empty()) return{};
vector<int> res, m1(, ), m2(, );
for(int i=; i<p.size(); i++)
{
m1[p[i]]++;
m2[s[i]]++;
}
if(m1==m2) res.push_back();
for(int i=p.size(); i<s.size(); i++)
{
m2[s[i]]++;
m2[s[i-p.size()]]--;
if(m1==m2) res.push_back(i-p.size()+);
}
return res;
}
};
参考
1. Leetcode_438. Find All Anagrams in a String;
完
【leetcode】438. Find All Anagrams in a String的更多相关文章
- 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode❤python】 438. Find All Anagrams in a String
class Solution(object): def findAnagrams(self, s, p): """ :type s: s ...
- 【LeetCode】1417. 重新格式化字符串 Reformat The String
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode- ...
- 【LeetCode】1408. 数组中的字符串匹配 String Matching in an Array
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetco ...
- 【LeetCode】387. First Unique Character in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
随机推荐
- 剑指offer(49)把字符串转换成整数。
题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串,包括数字字母符号,可以为空 输出描述: 如果是合法 ...
- codeforce 955c --Sad powers 思路+二分查找
这一题的题意是 定义一个数,该数特点是为a的p次方 (a>0,p>1) 再给你n个询问,每个询问给出一个区间,求区间内该数的数目. 由于给出的询问数极大(10e5) 所以,容易想到应该 ...
- Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误
Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误,一看,还缺 ...
- nodejs web API 相关杂项
场景是这样的: docker-compose中起多个服务,其中有一个是nodejs写的作为web API. 这个API 的使用者有2类: 1 docker-compose网络内其他特定服务访问,作为C ...
- Orchard是如何呈现内容的
首先Orchard是一个建立在ASP.NET MVC框架上的CMS应用框架.Orchard在呈现内容的时候也遵循MVC的规律,也是通过Controller来处理Url请求并决定用那个View来呈现那种 ...
- HTML 点击图片放大
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 把一个对象赋值给另一个对象会指向同一个内存地址
先看一段代码: var arr1 = [1,2,3]; var arr2 = arr1; arr2.push(4); console.log(arr1)//[1,2,3,4] 为什么会输出 的是[1, ...
- @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
@ControllerAdvice 和 @ExceptionHandler 的区别 ExceptionHandler, 方法注解, 作用于 Controller 级别. ExceptionHandle ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- 2、使用Angular-CLI初始化Angular项目(踩过的深坑!!!)
1.step1:建一个放项目的文件夹,打开cmd,或vs code的终端,找到文件夹根目录 2.step2:初始化脚手架 初始化命令: ng new 项目名称 --skip-install 注意:-- ...