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. 最短路问题--Floyd 畅通工程续

    畅通工程续 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很 ...

  2. TCP连接为什么三次握手四次挥手

    前几天面试某电商被问住了,问的很细,我就说了说连接过程,必然凉凉.在csdn上找了一篇很详细的博客.https://blog.csdn.net/hyg0811/article/details/1023 ...

  3. css伪元素::before与::after使用基础示例

    1.指定文本前后添加内容 <div class="box">test</div> .box::before{ content: 'before'; marg ...

  4. Channels(纪念一下卡我心态的一道题)

    链接:https://ac.nowcoder.com/acm/contest/3947/C来源:牛客网 题目描述 Nancy喜欢学习,也喜欢看电视. 为了想了解她能看多长时间的节目,不妨假设节目从时刻 ...

  5. 配置Action

    配置Action 实现了Action类后,就可以在struts.xml中配置该Action类了.配置Action就是让Struts2 知道哪个Action处理哪个请求,也就是完成用户请求和Action ...

  6. 题解 P3117 【[USACO15JAN]牛的矩形Cow Rectangles】

    暴力什么的就算了,贪心他不香吗 这题其实如果分开想,就三种情况需要讨论:(由于不会发图,只能手打) 1) 5 . . . . . 4 . . . . . 3 . . . H . 2 . . G . . ...

  7. pandas在指定列插入数据

    import pandas as pd import numpy as np df = pd.DataFrame(np.arange(15).reshape(5, 3), columns=['a', ...

  8. python中的倒序遍历

    1.在列表本身倒序 a = [1, 3, 7, 5, 2, 6] a.reverse() # 在列表本身进行倒序,不返回新的值 print(a) # 输出a: # [6, 2, 5, 7, 3, 1] ...

  9. 并发与高并发(四)-java并发的优势和风险

  10. console.log和alert的区别

    alert是同步的,如果不关闭弹出框,js代码就不会继续执行下去,这时候浏览器啥都干不了. console.log不会打断js的执行. 当要输出几十几百条信息的时候还是得用console.log,而且 ...