Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

Example 1:

Input:
s: "cbaebabacd" p: "abc" Output:
[0, 6] Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input:
s: "abab" p: "ab" Output:
[0, 1, 2] Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab". Time: O(N)
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]: my_dict = {}
res = []
count = 0
for char in p:
freq = my_dict.get(char, 0)
my_dict[char] = freq + 1 for i in range(len(s)):
char = s[i]
if char in my_dict:
my_dict[char] -= 1
if my_dict[char] == 0:
count += 1 if i >= len(p):
start = i - len(p)
start_char = s[start]
if start_char in my_dict:
my_dict[start_char] += 1
if my_dict[start_char] == 1:
count -= 1
# check my_dict size instead of len(p)
if count == len(my_dict):
res.append(i - len(p) + 1)
return res
public class Solution {
public List<Integer> allAnagrams(String sh, String lo) {
// Write your solution here
Map<Character, Integer> map = new HashMap<>();
char[] charArr = sh.toCharArray();
for (int i = 0; i < charArr.length; i++) {
map.put(charArr[i], map.getOrDefault(charArr[i], 0) + 1);
} List<Integer> res = new ArrayList<>();
char[] lCharArr = lo.toCharArray();
int count = 0;
int start = 0;
for (int i = 0; i < lCharArr.length; i++) {
char cur = lCharArr[i];
if (map.containsKey(cur)) {
int num = map.get(cur);
if (num == 1) {
count += 1;
}
map.put(cur, num - 1);
} if (i >= sh.length()) {
start = i - sh.length();
if (map.containsKey(lCharArr[start])) {
int startNum = map.get(lCharArr[start]);
if (startNum == 0) {
count -= 1;
}
map.put(lCharArr[start], startNum + 1);
}
} if (count == map.size()) {
res.add(i - sh.length() + 1);
}
}
return res;
}
}

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

    原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...

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

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

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

  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. 用户体验评价——win10自带微软拼音输入法

    目前正在使用的一款输入法就是win10自带的微软拼音输入法, 用户界面,一直在用个人感觉最大的特点就是十分简洁,界面的布局十分清晰,可以隐藏悬浮窗让他显示在菜单栏, 另外其记住用户输入习惯方面也十分优 ...

  2. filter的原理(转)

    今天学习了一下javaweb开发中的Filter技术,于是在网上搜了一下相关资料,发现这篇博客写的很不错,于是希望能转载过来以备以后继续学习之用.(原:http://www.cnblogs.com/x ...

  3. Idea Spring工程不识别注解

    如图所示 Idea工具报出很多注解不识别,开始怀疑是 工具问题,重装Idea.配置lombak都不行,切换分支发现正常,一定是合入代码修改啥了,一行行比对,果然是这行 import org.sprin ...

  4. 通过geopandas.sjoin()函数按多边形范围分割点

    最近有一批点和多变型的数据,需要将点按照多边形的区域进行分割. 经过若干尝试,终于通过geopandas的sjoin函数得以实现. 这里首先感谢博主“张da统帅”的分享,使得本人获得该实现方法的灵感, ...

  5. MVC——EF 回顾总结

    回顾一下MVC的知识点. 其实开始 我在学校的知识对MVC 还是很模糊的一个概念.只是记得结合EasyUI 增删改查 和分页,代码都是模糊的 进过这段时间的学习,让我对MVC 有了一个很清楚的认识. ...

  6. D11 列表 list 元祖 字典dict

    取值 name = "alexdfg" print(name[3:5]) 取出 ex name = "alexdfg" print(name[3]) 取出e 列 ...

  7. elasticsearch-logstash

    1.logstash介绍 logstash 是ES 下的一款开源软件.用于数据采集,就是从Mysql等数据源采集数据.更新数据.然后将数据发送到ES中创建.更新索引 2.安装 演示环境是windows ...

  8. Linux(CENTOS7) Nginx负载均衡简单配置

    负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正常工 ...

  9. MySql、Mongodb和Redis的区别

    NoSQL 的全称是 Not Only SQL,也可以理解非关系型的数据库,是一种新型的革命式的数据库设计方式,不过它不是为了取代传统的关系型数据库而被设计的,它们分别代表了不同的数据库设计思路. M ...

  10. UML-如何进行对象设计?

    之前的章节,学过了OOA,以及交互图+类图.本章主要讲述OOD.OOD就是面向对象设计,那如何进行对象设计? 概览 1.输入制品 注:这些制品并非都必要. 2.活动 1).针对输入的制品,采用什么样的 ...