[LC] 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 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的更多相关文章
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- [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 ...
- [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 ...
- 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 ...
- 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
随机推荐
- 2019.9.30极限测试 04.JAVA语言课堂测试试卷-极限测试
题目存储在上传的文件当中. 代码实现 Subway 类: package ClassroomTest; public class Subway { private String railway; pr ...
- kali由wifi握手包破解密码&&gnuplot使用
1.kali密码破解(WiFi握手包) cap包密码破解,aircrack-ng wifi.cap -w psw.txt(你的字典文件) 2.画图工具gnuplot 1.txt中保存的是坐标,形式为: ...
- Linux中Tomcat 自动设置CATALINA_HOME方法
Linux中Tomcat 自动设置CATALINA_HOME方法 在服务器部署中,我们经常会出现“在一个服务器上运行多个tomcat服务”的情况. 使用如下方法设置,可以无限复制平移扩展Tomcat, ...
- JAVE官方文档
官网地址:http://www.sauronsoftware.it/projects/jave/manual.php JAVE manual Installation and requirements ...
- Python笔记_第四篇_高阶编程_再议装饰器和再议内置函数
1. 概述: 我们在前面用了很多的装饰器这个工具的方法.这个位置要系统的讲一下装饰器. 1.2 为什么需要装饰器. 装饰器本质是一个Python函数,它可以让其他函数在不需要任何代码变动的前提下增加额 ...
- C++构造函数概念作用
作用: 对对象进行初始化,如给成员变量赋初值,而不用专门再写初始化函数. 防止有些对象没被初始化就使用,导致程序出错. 要求: 名字与类名相同,可以有参数,但不能有返回值(void也不行) 编译时: ...
- Python登录TP-Link路由器换ip脚本
有些时候我们需要更换IP(你懂得),网络下载的拨号软件大部分是需要电脑直接链接调制解调器(猫),对于局域网用户来说就比较麻烦了,下面我们用python来实现登录路由器自动切换ip的功能 # -*- c ...
- php多态模拟
在PHP中,多态是最常用到的一种特性.所谓多态,是指同一个东西不同形态的展示.在PHP中,我们这样定义多态,一个类被多个子类继承,如果这个类的某个方法在多个子类中表现不同的功能,那么这种行为我们就称其 ...
- 吴裕雄--天生自然 PHP开发学习:MySQL 读取数据
<?php $servername = "localhost"; $username = "root"; $password = "admin& ...
- 使用hexo+GitHub搭建个人博客的心得(含教程)
Author Email Yaoyao Liu yaoyaoliu@msn.com 前言 对于广大CS专业的学生和码农,找一个地方写博客,记录一些编程.配置环境.阅读论文的心得体会是一个很常见的习惯. ...