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的更多相关文章

  1. 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...

  2. 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词

    Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...

  3. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  4. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  5. 【leetcode❤python】 438. Find All Anagrams in a String

    class Solution(object):    def findAnagrams(self, s, p):        """        :type s: s ...

  6. 【LeetCode】1417. 重新格式化字符串 Reformat The String

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode- ...

  7. 【LeetCode】1408. 数组中的字符串匹配 String Matching in an Array

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetco ...

  8. 【LeetCode】387. First Unique Character in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】434. Number of Segments in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...

随机推荐

  1. Bugku-CTF之变量1

    Day9 变量1 http://123.206.87.240:8004/index1.php      

  2. 微信小程序wepy开发,属性绑定的事件参数中可以使用{{}}写实参

    <view wx:for="{{tablist}}" class="item {{activeid === item.id ? 'active':''}}" ...

  3. Vue提供操作DOM的方法

    <div ref="wrapper"> Vue.js 提供了我们一个获取 DOM 对象的接口—— vm.$refs.在这里,我们通过了 this.$refs.wrapp ...

  4. 【笔记】Cocos2dx学习笔记

    自建场景类 自建场景类BaseScene继承与Scene类,在init函数中添加了默认的,键盘与鼠标事件的响应,添加了一个用于读取XML文件的字典,添加了一个结束场景的方法. 类的声明代码如下: #i ...

  5. ActiveReports 大数据分析报告:2018中国电影再次迎来黄金时代

    回顾2018,中国电影市场收获颇丰.先是凭借春节档<红海行动>.<唐人街探案>双双实现30亿票房突破,而后暑期档火力全开,<我不是药神>.<西虹市首富> ...

  6. centos install jdk

    =========== 查询jdk版本 ===========yum search jdk =========== 安装jdk 64位开发版 ===========yum -y install jav ...

  7. WordPress 本地建站

    1.搭建环境 appserv下载链接:http://www.onlinedown.net/soft/35753.htm 安装 1.直接运行 2.选择安装路径 3.选择所需环境,若已经有,则可不勾选 4 ...

  8. ES6之Array数组

    定义数组 ,]; const arr = new Array(1,2,3,4); const array1 = new Array(); array1[]="test"; 给数组不 ...

  9. webpack简单修改版本号(单页面)

    写了一个js文件,可以尽量最简单的修改版本号 package.json配置: updateV.js放置位置: updateV.js: var fs = require('fs'); //文件读写 va ...

  10. tensorflow和keras混用

    在tensorflow中可以调用keras,有时候让模型的建立更加简单.如下这种是官方写法: import tensorflow as tf from keras import backend as ...