当前位置:网站首页>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;
}
}
边栏推荐
- excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- On the setting of global variable position in C language
- AcWing 787. 归并排序(模板)
- cres
- Methods of checking ports according to processes and checking processes according to ports
- Monotonic stack -503 Next bigger Element II
- JS non Boolean operation - learning notes
- 拯救剧荒,程序员最爱看的高分美剧TOP10
- LeetCode 715. Range 模块
猜你喜欢

LeetCode 57. 插入区间

LeetCode 438. 找到字符串中所有字母异位词

Try to reprint an article about CSDN reprint

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

MySQL three logs

AcWing 788. 逆序对的数量

请求参数的发送和接收

Facial expression recognition based on pytorch convolution -- graduation project

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

JS non Boolean operation - learning notes
随机推荐
cres
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
Complex character + number pyramid
ES6 promise learning notes
求组合数 AcWing 885. 求组合数 I
Common penetration test range
Find the combination number acwing 885 Find the combination number I
LeetCode 535. TinyURL 的加密与解密
Mortgage Calculator
Concurrent programming (III) detailed explanation of synchronized keyword
一个优秀速开发框架是什么样的?
[concurrent programming] collaboration between threads
20220630 learning clock in
LeetCode 715. Range 模块
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
[concurrent programming] concurrent security
PHP function date (), y-m-d h:i:s in English case
The method of replacing the newline character '\n' of a file with a space in the shell
[rust notes] 08 enumeration and mode
Servlet的生命周期