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. zTree & ckeditor &ValidateCode.jar 使用个人心得总结

    zTree:依靠 jQuery 实现的多功能 “树插件”. 使用时只需要将下载的压缩包接用,复制里边的css 和 js 到指定目录即可. 如图所示: 在zTree的官网可以找到各种类型树的示例: 地址 ...

  2. PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  3. Hadoop的常用指令

    -help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...

  4. FPGA的基本组成单元LUT,以及三种核的概念

    .查找表 LUT就是查找表,对于4输入的LUT而言,实际上就是4位地址位,一位数据位的存储器,能够存储16位数据,所以我们在FPGA设计中可以用LUT组建分布式的RAM. 这样也可以解释我们在设计中为 ...

  5. 12)hInstance和hWnd写进子类

    1)因为这些变量存在于 WInMaincpp文件中  但是  我想在我的CGameCtrl子类中要用到hInstance实例句柄和hWNd窗口句柄,那么 我就将这些变量在父类CGameCtrl中有一份 ...

  6. 吴裕雄--天生自然ShellX学习笔记:Shell 基本运算符

    Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...

  7. Constant expression required

    switch 语句编译报错Constant expression required 问题:case语句后面必须是常量,使用final修饰 public static final int NotifyT ...

  8. android studio 修改新建EmptyActivity默认布局

    https://www.jianshu.com/p/d4f201135097 打开你的Android Sudio安装目录,我的为D:\Program Files\Android\Android Stu ...

  9. UML-逻辑架构&包图-相关概念

    1.逻辑架构 软件的宏观组织结构.含: 1).包 2).子系统 3).层 2.层 对类.包.子系统的分组(内聚).例如:mvc.在OOA/D中要重点关注核心应用逻辑(或领域)层. 3.UML包图 描述 ...

  10. Unity中的事件监听

    Unity3D的uGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值.如下图所示: 先以PointerClick为例.这个是用于某点点击事件.其他事件都可 ...