原题:

438. Find All Anagrams in a String

解题:

两个步骤

1)就是从s中逐步截取p长度的字符串

2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这两种方法提交后都超时了)

代码如下(提交超时):

class Solution {
public:
vector<int> findAnagrams(string s, string p)
{
int lenp = p.length();
int lens = s.length();
int i=0,j,flag; vector <int> result;
map <char,int> mapstr;
map <char,int> maptemp;
for(j =0; j < lenp;j++)
{
mapstr[p[j]]++;
}
while(i < lens)
{
maptemp = mapstr;
flag = true;
for(j = i; j < i +lenp;j++)
{
if(--maptemp[s[j]] < 0)
{
flag = false;
break;
}
}
if(flag)
{
result.push_back(i);
}
i++;
}
return result;
}
};

  提交AC:

class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if (s.empty()) return {};
vector<int> res, m(256, 0);
int left = 0, right = 0, cnt = p.size(), n = s.size();
for (char c : p) ++m[c];
while (right < n) {
if (m[s[right++]]-- >= 1) --cnt;
if (cnt == 0) res.push_back(left);
if (right - left == p.size() && m[s[left++]]++ >= 0) ++cnt;
}
return res;
}
};

  

438. Find All Anagrams in a String的更多相关文章

  1. 【leetcode】438. Find All Anagrams in a String

    problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...

  2. 438. Find All Anagrams in a String - LeetCode

    Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. [LC] 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 ...

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

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

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

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

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

    详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...

随机推荐

  1. ubuntu16.04下python2、python3环境选择与python升级(pip版本切换)

    参考链接:https://www.jianshu.com/p/63c1f22e1fed Ubuntu中python版本问题: 添加PPA: sudo add-apt-repository ppa:jo ...

  2. 开源中国/码云 README.md上传图片的爬坑记录

    整理代码,将电脑中长期没有用过的代码放到码云上托管,给项目录制gif动画,在写项目README.md时使用,结果在上传图片时一直出问题,现记录下最后解决方法: 1. 最初直接将录制好的图片放入到img ...

  3. Ubuntu16.04中pip无法更新升级,采用源码方式安装

    1.从pip官网下载最新版 https://pypi.org/project/pip/#files 2.ubuntu中创建文件位置,我的放在一下路径,之后进行解压 3.解压后进入pip的文件夹,在执行 ...

  4. lucas 模板 洛古模板题

    #include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int quick(in ...

  5. 开源WHMCS支付宝当面付和即时到账插件

    开源WHMCS支付宝当面付和即时到账插件 链接: https://pan.baidu.com/s/1i5HU4hn 密码: crq7

  6. Java 初始 多态

    什么是多态 简单的来说就是具有多种形态的能力的特征 package ten; public interface Day1 { public void ring(); } package ten; pu ...

  7. PythonStudy——赋值运算符 Assignment operator

    eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...

  8. oracle-rman-3

    http://blog.csdn.net/leshami/article/details/6032525 rman概述及体系结构 http://blog.itpub.net/23513800/view ...

  9. jQuery基础(二)DOM

    DOM节点的创建 jQuery节点创建与属性的处理 创建元素节点: $("<div></div>") 创建为文本节点: $("<div> ...

  10. 对象属性的描述:writable、enumerable、configurable

    writable属性 writable属性是一个布尔值,决定了目标属性的值(value)是否可以被改变.如果原型对象的某个属性的writable为false,那么子对象将无法自定义这个属性. enum ...