原题:

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. Socket网络编程入门

    Socket:专业术语:套接字;通俗的解释:两孔插座(一个孔:IP地址,一个孔:端口号).使用场景:通信,如QQ好友交谈,如浏览器的进程怎么与web服务器通信等. Socket来历: socket起源 ...

  2. 神州数码OSPF基于端口认证(简单认证、MD5认证)

    实验要求:掌握OSPF基于端口认证方式 拓扑如下 简单认证 R1 enable 进入特权模式 config  进入全局模式 hostname R1 修改名称 interface s0/1 进入端口 i ...

  3. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  4. Linux进程调度与抢占

    一.linux内核抢占介绍 1.抢占发生的必要条件 a.preempt_count抢占计数必须为0,不为0说明其它地方调用了禁止抢占的函数,比如spin_lock系列函数.b.中断必须是使能的状态,因 ...

  5. url集合

    restful方面 Java后台框架篇--Spring与Restful风格API接口开发 https://blog.csdn.net/hello_worldee/article/details/781 ...

  6. 乐乐课堂_leleketang.com

    乐乐课堂_leleketang.com https://www.baidu.com/sf?pd=video_page&sign=12394301609542619800&word=抛物 ...

  7. Zuul 跨域

    JS访问会出现跨域问题的解决, 一.对单个接口,处理跨域,只需要在被调用的类或或方法增加注解 CoossOrigin 如下设置 allowCredenticals=true,表示运行Cookie跨域 ...

  8. Excel技巧--实现交叉查询

    如上图,要实现某个地区和某个产品的销售额查询显示.可以使用Match和Index函数的使用来实现: 1.产品名称和城市栏,制作成列表可选:使用“数据”-->“数据验证”的方法. 2.先在旁边空位 ...

  9. Excel技巧--分隔工资条

    要将上图的工资表,做成每行都带标题而且有空行间隔的工资条,可以这么做: 1.表格右侧添加一列数据列:输入1,2,选定,并双击单元格右下角形成一升序数字列: 2.再将该列复制,粘贴到该列末尾: 3.点一 ...

  10. 第一天Python

    一.开发语言 高级语言:Python  Java.PHP     高级语言--字节码(PHP适用于写网页) 低级语言:C.汇编--机器码(底层开发,根本,效率低) 二.Python种类 三.安装