当前位置:网站首页>LeetCode 438. Find all letter ectopic words in the string
LeetCode 438. Find all letter ectopic words in the string
2022-07-03 09:02:00 【Sasakihaise_】
438. Find all alphabetic words in the string

【 violence 】 Count with array p The number of occurrences of each letter in , Match from beginning to end , It is equivalent to a sliding window with variable length and restriction ,O(n^2) Time complexity of , Barely make it through .
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;
}
}【 The sliding window 】 Fixed length sliding window , Statistics p Length of the number of characters in the window , Remove the previous characters every time you move , Plus the following characters , And p Compare the number of Chinese characters , The time complexity is reduced to O(n*26)
class Solution {
// The sliding window 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;
}
}
边栏推荐
- LeetCode 1089. 复写零
- Really explain the five data structures of redis
- [rust notes] 09- special types and generics
- Summary of methods for counting the number of file lines in shell scripts
- C language student management system based on linked list, super detailed
- Using DLV to analyze the high CPU consumption of golang process
- Facial expression recognition based on pytorch convolution -- graduation project
- too many open files解决方案
- Dom4j遍历和更新XML
- Analysis of Alibaba canal principle
猜你喜欢

LeetCode 1089. 复写零

Dom4j traverses and updates XML

Character pyramid

JS ternary operator - learning notes (with cases)

网络安全必会的基础知识

Binary tree sorting (C language, int type)

Wonderful review | i/o extended 2022 activity dry goods sharing

LeetCode 515. 在每个树行中找最大值

Try to reprint an article about CSDN reprint

PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
随机推荐
LeetCode 715. Range 模块
String splicing method in shell
网络安全必会的基础知识
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Annotations simplify configuration and loading at startup
<, < <,>, > > Introduction in shell
高斯消元 AcWing 883. 高斯消元解线性方程组
Use of sort command in shell
浅谈企业信息化建设
XPath实现XML文档的查询
Slice and index of array with data type
[concurrent programming] collaboration between threads
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
我们有个共同的名字,XX工
求组合数 AcWing 886. 求组合数 II
AcWing 788. 逆序对的数量
Life cycle of Servlet
我們有個共同的名字,XX工
Markdown learning