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".
这个题目我最开始的思路是d2 = collections.Counter(p), 然后d1 = collections.Counter(s[:len(p)]), 然后每次往后移动的时候, 就改变相应的d1, 然后比较, 但是发现用Counter比较的时候有个问题, 因为d1 有可能有'c':0 的情况, 虽然跟d2是一样, 但是并不能相等. 所以用[0]*26来代替Counter, 只是比较的时候要用ord(c) - ord('a')去得到index
Code
class Solution:
def findAnagrams(self, s, p):
l_s, l_p, ans = len(s), len(p), []
if l_p > l_s: return ans
d1, d2 = [0]*26, [0]*26 # 只有小写
for i in range(l_p):
d1[ord(s[i]) - ord('a')] += 1
d2[ord(p[i]) - ord('a')] += 1
if d1 == d2: ans.append(0)
for i in range(1, l_s-l_p + 1):
d1[ord(s[i-1]) - ord('a')] -= 1
d1[ord(s[i+ l_p -1]) - ord('a')] += 1
if d1 == d2:
ans.append(i)
return ans
[LeetCode] 438. Find All Anagrams in a String_Easy的更多相关文章
-
[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
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
-
【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
-
438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
-
【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
-
【leetcode❤python】 438. Find All Anagrams in a String
class Solution(object): def findAnagrams(self, s, p): """ :type s: s ...
-
【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
随机推荐
-
MacDown语法教程
MacDown Hello there! I'm MacDown, the open source Markdown editor for OS X. Let me introduce myself. ...
-
C指针-数组和指针的归一
int bArr[] = {1,2,3}; int *iarr = bArr; *iarr = 6; printf("%d\n",*iarr); printf("%d\n ...
-
LoadRunner 场景运行error的几种情况
一. Error -27727: Step download timeout (120 seconds)has expired when downloading resource(s). Set th ...
-
Automotive Security的一些资料和心得(7):AUTOSAR和Security
1. 密码模块[1] 密码模块在Services Layer Configurable and common access to 密码子程序 硬件支持密码模块 2. 应用 应用和密码子程序分离 Cry ...
-
浏览器因cookie设置HttpOnly标志引起的安全问题
1.简介 如果cookie设置了HttpOnly标志,可以在发生XSS时避免JavaScript读取cookie,这也是HttpOnly被引入的 原因.但这种方式能防住攻击者吗?HttpOnly标志可 ...
-
Oracle集合操作函数:union、intersect、minus
[转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...
-
Lambda高手之路第一部分
转http://www.cnblogs.com/lazycoding/archive/2013/01/06/2847574.html 介绍 Lambda表达式是使代码更加动态,易于扩展并且更加快速(看 ...
-
ajax解决跨域问题
1.在介绍之前先介绍几个概念 json: { date: "Sun Dec 24 21:44:42 CST 2017", temperature: "21", ...
-
怎么解决VMware“该虚拟机似乎正在使用中”问题
问题如下:
-
MySQL 5.6的一个bug引发的故障
突然收到告警,提示mysql宕机了,该服务器是从库.于是尝试登录服务器看看能否登录,发现可以登录,查看mysql进程也存在,尝试登录提示 ERROR (HY000): Too many connect ...