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. VUE- iView组件框架的使用

    VUE- iView组件框架的使用 1. 下载iView 工程. 引用:https://www.iviewui.com/

  2. 雅可比行列式【2】Jacobian行列式的意义

    2.1 线性变换将面积伸缩 对于一个\(\R^2\to\R^2\)的线性变换: \[ T(x,y)= \left[ \begin{array}{c} 4x-2y\\ 2x+3y \end{array} ...

  3. IDEA抽取方法的快捷键

    正常的话是 ctrl+alt+m 如果快捷键占用或者修改过,在写代码的地方右键->refactor->extract->method

  4. salt-stack 安装nginx

    init-pkg-install: pkg.installed: - names: - gcc - gcc-c++ - make - autoconf - openssl - openssl-deve ...

  5. nginx_tcp_proxy代理酸酸乳

    一.安装低版本的nginx(高版本不支持tcp代理模块:nginx_tcp_proxy_module)Nginx默认只支持http反向代理,要支持tcp反向代理,需在编译时增加tcp代理模块[ngin ...

  6. php抓取网站图片源码

    <?php /*完成网页内容捕获功能*/ function get_img_url($site_name){     $site_fd = fopen($site_name, "r&q ...

  7. tensorflow函数解析:Session.run和Tensor.eval

    原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...

  8. spring-boot 如何加载rsources下面的自定义配置文件

    spring-boot 如何加载resources下面的自定义配置文件 https://segmentfault.com/q/1010000006828771?_ea=1144561

  9. python学习笔记-函数与可变长参数

    一.函数 1. def test(x): y=x+1 return yprint(test) #这是打印函数在内存中的地址 过程:就是没有返回值的函数 在python中过程也是函数,就算没哟返回值,也 ...

  10. SpringBoot项目示例

    一.准备工作: 环境及工具:Eclipse+JDK8+Maven+SpringBoot 二.源码: 1.项目架构:                    2.引入pom.xml <project ...