当前位置:网站首页>LeetCode 438. 找到字符串中所有字母异位词
LeetCode 438. 找到字符串中所有字母异位词
2022-07-03 08:49:00 【Sasakihaise_】

【暴力】用数组统计p中各个字母的出现次数,从头到尾匹配,相当于一个变长带限制的滑动窗口,O(n^2)的时间复杂度,勉强能过。
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> ans = new ArrayList();
int m = s.length(), n = p.length(), l = m - n, j;
int[] cnt = new int[26];
for(var i = 0; i < n; i++) cnt[p.charAt(i) - 'a']++;
for(var i = 0; i <= l; i++){
boolean flag = true;
int[] tmp = cnt.clone();
for(j = 0; j < n; j++){
char c = s.charAt(i + j);
if(tmp[c - 'a'] == 0){
flag = false; break;
}else{
tmp[c - 'a']--;
}
}
if(flag) ans.add(i);
}
return ans;
}
}【滑动窗口】 定长滑动窗口,统计p长度的窗口内字符的个数,每次移动时把前面字符去掉,再加上后面的字符,与p中字符个数进行比对,时间复杂度降到O(n*26)
class Solution {
// 滑动窗口 1:28
public List<Integer> findAnagrams(String s, String p) {
int m = s.length(), n = p.length(), l = m - n;
int[] cnt = new int[26], wd = new int[26];
List<Integer> ans = new ArrayList();
if(m < n) return ans;
for(var i = 0; i < n; i++) cnt[p.charAt(i) - 'a']++;
for(var i = 0; i < n; i++) wd[s.charAt(i) - 'a']++;
if(Arrays.equals(cnt, wd)) ans.add(0);
for(var i = n; i < m; i++){
wd[s.charAt(i - n) - 'a']--;
wd[s.charAt(i) - 'a']++;
if(Arrays.equals(cnt, wd)) ans.add(i - n + 1);
}
return ans;
}
}
边栏推荐
- Memory search acwing 901 skiing
- JS ternary operator - learning notes (with cases)
- PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
- Baidu editor ueeditor changes style
- [rust notes] 02 ownership
- Deep parsing (picture and text) JVM garbage collector (II)
- cres
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- createjs easeljs
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
猜你喜欢

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

DOM render mount patch responsive system

Analysis of Alibaba canal principle

Binary tree sorting (C language, char type)

Phpstudy 80 port occupied W10 system

Mortgage Calculator

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

【Rust笔记】02-所有权

Notes and bugs generated during the use of h:i:s and y-m-d

LeetCode 241. 为运算表达式设计优先级
随机推荐
Unity notes 1
树形DP AcWing 285. 没有上司的舞会
[rust notes] 05 error handling
XPath实现XML文档的查询
数据库原理期末复习
【Rust 笔记】13-迭代器(上)
状态压缩DP AcWing 291. 蒙德里安的梦想
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
[concurrent programming] Table hopping and blocking queue
Gif remove blank frame frame number adjustment
JS ternary operator - learning notes (with cases)
[concurrent programming] thread foundation and sharing between threads
22-05-26 Xi'an interview question (01) preparation
MySQL index types B-tree and hash
[RPC] RPC remote procedure call
Drawing maze EasyX library with recursive backtracking method
Summary of methods for counting the number of file lines in shell scripts
createjs easeljs
状态压缩DP AcWing 91. 最短Hamilton路径
Using variables in sed command