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;
}
}

最新文章

  1. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
  2. 一个事务复制的bug--更新丢失 续
  3. jQuery .on() 绑定事件无效
  4. 1.padding和margin,几种参数
  5. .net format 中 大括号{}处理
  6. request、response的setCharacterEncoding与response的setContentType
  7. Javascript高级程序设计复习——第五章引用类型 【原创】
  8. centos服务器如何监控访问ip,并将非法ip通过防火墙禁用
  9. 行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例
  10. Gradient Boosting, Decision Trees and XGBoost with CUDA ——GPU加速5-6倍
  11. linux 中 &amp;&amp; 及|| 判断原理
  12. 洛谷P2765魔术球问题 最小路径覆盖
  13. 拓扑排序bfs_dfs
  14. Web(一)
  15. 对vue生命周期的理解
  16. My sql 自增 虚拟列。
  17. JSP中scope属性 scope属性决定了JavaBean对象存在的范围
  18. H-a
  19. Cocos2d-x 3.1.1 学习日志3--C++ 初始化类的常量数据成员、静态数据成员、常量静态数据成员
  20. Android Test和Logcat

热门文章

  1. UVA 11732 链表+字典树
  2. gulp 学习入门
  3. 吴裕雄--天生自然MySQL学习笔记:MySQL 导出数据
  4. 51nod A 魔法部落(逆元费马小定理)
  5. ZJNU 2345 - 小Y的方格纸
  6. 题解【[BJOI2012]算不出的等式】
  7. nginx_tcp_proxy代理酸酸乳
  8. Thinkcmf子栏目获取父级栏目所有子栏目列表
  9. 吴裕雄--天生自然ShellX学习笔记:Shell 传递参数
  10. 实践一次有趣的sql优化