子串字谜substring anagrams
[抄题]:
给定一个字符串 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)--
[一刷]:
- 字符串处理一个个的字母时,要用.tochararray先把字符串转成数组
- det也是数组,absSum是其的绝对值求和
- det 有几个元素算几个,用for (int item : det)简写
- s.toCharArray();表示对方法的调用
- list一定要用linkedlist or arraylist来实现,不能直接复制粘贴
[二刷]:
- 特殊判断必须要写
- 搞清楚det的含义 = sc - pc pc增加,det = r - l r增加
- 在之后的数组中,都是计算cntS[] 数组的数量
- absSum要用Math.abs
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
根据表格进行联想
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
242. Valid Anagram 用deta求absSum
567. Permutation in String 两根指针?不懂
[代码风格] :
- 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的更多相关文章
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- [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 ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- Substring Anagrams
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- [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 ...
- [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 ...
- [Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口
[题目] Given a string, find the length of the longest substring without repeating characters. [举例] Exa ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- 最长无重复字符的子串 · Longest Substring Without Repeating Characters
[抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...
随机推荐
- 使用Maven简单配置Mybatis
1.新建一个Maven项目 2. 在pom.xml中进行配置,在pom.xml中配置的时候,需要网速好,当网速不是很好的时候,是加载不出Jar包的. 代码如下所示. <project xmlns ...
- DELL E7240超极本
一.图片展示 1.整体图 2.扩展坞的后卡槽附带挡板 3.电源线 4.边框指示灯 5.平躺展示摄像头 二.参数配置 1.i5的 2.i7的 3.手写 处理器:英特尔四代处理器 i7-4600U双 ...
- NET Core 实战:使用 NLog 将日志信息记录到 MongoDB
NET Core 实战:使用 NLog 将日志信息记录到 MongoDB https://www.cnblogs.com/danvic712/p/10226557.html ASP.NET Core ...
- test20181024 hao
题意 分析 考场10分 直接\(O(nm)\)模拟即可. #include<cstdlib> #include<cstdio> #include<cmath> #i ...
- vue使用百度编辑器ueditor踩坑记录
一.使用 下载放入项目 main.js引入 import '../static/UE/ueditor.config.js'; import '../static/UE/ueditor.all.js'; ...
- xargs命令学习
1.xargs复制文件 目录下文件结构为: . ├── demo1 │ ├── test.lua │ ├── test.php │ └── test.txt └── demo2 执行命令: find ...
- 黄聪:VS2010编辑C#未启动,打开设计视图时报"未将对象引用设置到对象的实例"
通常情况下,若是你将用户控件写好了放入窗体中,若是有不合理的代码,则会弹出错误提示框,不让你放.若是你之前只是随便加了一个用户控件,并且没有什么问题,但后来你又把控件改坏掉了,那么你打开就会报错(在窗 ...
- mysql-6正则表达式
mysql正则表达式 匹配的两种方式: 1.模糊匹配:like 2.正则表达式 正则表达式语法: 语法 说明 ^ 起始位置.如果设置了RegExp对象的Multiline属性,^也匹配'\n'或'\r ...
- RouterOS SOCKS代理服务器(官方文档翻译)
SOCKS 是基于TCP应用层协议穿透防火墙的代理服务器,即使防火墙阻止了一些应用端口,也能通过SOCKS代理穿透.SOCKS协议是独立于应用层的,因此可以用于WWW.FTP.Telnet等等. 来至 ...
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出 ...