当前位置:网站首页>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;
}
}
边栏推荐
- 干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- Phpstudy 80 port occupied W10 system
- Too many open files solution
- What is the difference between sudo apt install and sudo apt -get install?
- Analysis of Alibaba canal principle
- SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
- Annotations simplify configuration and loading at startup
- Format - C language project sub file
- ES6 promise learning notes
猜你喜欢
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
Gif remove blank frame frame number adjustment
Character pyramid
Wonderful review | i/o extended 2022 activity dry goods sharing
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
状态压缩DP AcWing 91. 最短Hamilton路径
Analysis of Alibaba canal principle
Deep parsing (picture and text) JVM garbage collector (II)
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
LeetCode 30. 串联所有单词的子串
随机推荐
我们有个共同的名字,XX工
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Monotonic stack -503 Next bigger Element II
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
Servlet的生命周期
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
高斯消元 AcWing 883. 高斯消元解线性方程组
Binary to decimal, decimal to binary
Redux - learning notes
Common penetration test range
Query XML documents with XPath
[rust notes] 12 closure
Binary tree sorting (C language, char type)
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
LeetCode 438. 找到字符串中所有字母异位词
第一个Servlet
Low code momentum, this information management system development artifact, you deserve it!
Baidu editor ueeditor changes style
注解简化配置与启动时加载