[抄题]:

给定一个字符串 s 和一个 非空字符串 p ,找到在 s 中所有关于 p 的字谜的起始索引。
字符串仅由小写英文字母组成,字符串 s 和 p 的长度不得大于 40,000。
输出顺序无关紧要.

样例

给出字符串 s = "cbaebabacd" p = "abc"
返回 [0, 6]

子串起始索引 index = 0 是 "cba",是"abc"的字谜.
子串起始索引 index = 6 是 "bac",是"abc"的字谜.

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

先初始化,再统计p位之后的绝对值之和。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

sliding window的原理是数数字,向右滑动时,count(r)++,count(l)--

[一刷]:

  1. 字符串处理一个个的字母时,要用.tochararray先把字符串转成数组
  2. det也是数组,absSum是其的绝对值求和
  3. det 有几个元素算几个,用for (int item : det)简写
  4. s.toCharArray();表示对方法的调用
  5. list一定要用linkedlist or arraylist来实现,不能直接复制粘贴

[二刷]:

  1. 特殊判断必须要写
  2. 搞清楚det的含义 = sc - pc pc增加,det = r - l r增加
  3. 在之后的数组中,都是计算cntS[] 数组的数量
  4. absSum要用Math.abs

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

根据表格进行联想

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

242. Valid Anagram 用deta求absSum

567. Permutation in String 两根指针?不懂

[代码风格] :

  1. cntS 才符合骆驼命名法
public class Solution {
/**
* @param s: a string
* @param p: a string
* @return: a list of index
*/
public List<Integer> findAnagrams(String s, String p) {
//initialization
List<Integer> ans = new LinkedList<>();
//corner case
if (s.length() < p.length()) {
return ans;
}
char[] sc = s.toCharArray();
char[] pc = p.toCharArray(); int[] cntS = new int[256];
int[] cntP = new int[256];
int[] det = new int[256];
//count first
int absSum = 0;
for (int i = 0; i < p.length(); i++) {
cntS[sc[i]]++;
cntP[pc[i]]++; det[sc[i]]++;
det[pc[i]]--;
}
for (int item : det) {
absSum += Math.abs(item);
}
if (absSum == 0) {
ans.add(0);
}
//count rest
for (int i = p.length(); i < s.length(); i++) {
int r = sc[i];
int l = sc[i - p.length()];
System.out.println("sc[i]="+sc[i]);
System.out.println("r="+r); cntS[r]++;//both s
cntS[l]--; absSum = absSum - Math.abs(det[r]) - Math.abs(det[l]);//abs det[l]--;
det[r]++; absSum = absSum + Math.abs(det[r]) + Math.abs(det[l]); if (absSum == 0) {
ans.add(i - p.length() + 1);
}
}
return ans;
}
}

子串字谜substring anagrams的更多相关文章

  1. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. [Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  3. LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...

  4. Substring Anagrams

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  5. [Swift]LeetCode159.具有最多两个不同字符的最长子串 $ Longest Substring with At Most Two Distinct Characters

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  6. [Swift]LeetCode340.最多有K个不同字符的最长子串 $ Longest Substring with At Most K Distinct Characters

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  7. [Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口

    [题目] Given a string, find the length of the longest substring without repeating characters. [举例] Exa ...

  8. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  9. 最长无重复字符的子串 · Longest Substring Without Repeating Characters

    [抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...

随机推荐

  1. 判断对称二叉树 python代码

    对称二叉树的含义非常容易理解,左右子树关于根节点对称,具体来讲,对于一颗对称二叉树的每一颗子树,以穿过根节点的直线为对称轴,左边子树的左节点=右边子树的右节点,左边子树的右节点=左边子树的左节点.所以 ...

  2. vue图片上传组件

    前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...

  3. RabbitMQ部署

    操作步骤: 安装依赖文件 -->yum install ncurses-devel 进入opt目录创建rabbitmq目录 -->cd /opt -->mkdir rabbitMQ ...

  4. 【java多线程】ConCurrent并发包 - Lock详解

    synchronized的缺陷   我们知道,可以利用synchronized关键字来实现共享资源的互斥访问. Java 5在java.util.concurrent.locks包下提供了另一种来实现 ...

  5. git推送报错: No path specified. See 'man git-pull' for valid url syntax或does not appear to be a git repository以及remote: error: insufficient permission for adding an object to repository databa

    本地(windows)代码想推送到linux自己搭建的git服务端,第一步是建立本地与服务端的关联,第二步是本地推送到服务端. 第一步需要看你的本地工程是否从git上clone来的,如果是clone来 ...

  6. 大型发布会现场的 Wi-Fi 应该如何搭建(密集人群部署wifi抗干扰)?

    原文连接: http://www.zhihu.com/question/20890194 WiFi网络的部署要远远比一般人想象的复杂,不是说放上几十个AP带宽就自动增加几十倍,恰恰相反,简单放几十个A ...

  7. VirtualBox的端口映射其实很好理解

    还是和以前百度的另一个知识点一样,我真不明白网上那些人要做什么,明明很简单的事,干嘛非要讲的那么复杂,就是为了让人觉得你很高手?很厉害? 名称:随便起的,基于好记的原则,你的什么应用在使用这一条端口转 ...

  8. 关于 android 环信无法正确获取昵称的问题

    本案例中 username 记录成 userId了, nick 始终为空...,这是 getNick() 取得的就是 username..... 如果想自己取得自己系统的nickname则 做以下调整 ...

  9. Java separatorChar 如何在Java里面添加 \

    Java手册 separatorChar public static final char separatorChar 与系统有关的默认名称分隔符.此字段被初始化为包含系统属性 file.separa ...

  10. fatal error: mysql.h: No such file or directory

    在ubuntu系统下安装mysql之后,和数据库连接的时候,出现如下错误:fatal error: mysql.h: No such file or directory 是因为缺少链接库,执行如下命名 ...