当前位置:网站首页>Find all letter ectopic words in the string
Find all letter ectopic words in the string
2022-07-22 10:03:00 【GreyZeng】
Find all alphabetic words in the string
author :Grey
Original address : Find all alphabetic words in the string
Title Description
LeetCode 438. Find all alphabetic words in the string
Main idea
Use Sliding windows and debt statements The mechanism of , First , take p
String to establish word frequency table
int c = pStr.length;
for (char ch : pStr) {
map[ch - 'a']++;
}
Used for query s
There is still... In the string p
What is the type and number of elements in the string . The total number of characters owed is c
.
Next, create a sliding window , stay s
Set in the string l
and r
Two pointers , Move first r
, If r
The character of the position is exactly p
The characters in , Explain that this is an effective repayment , be c--
,r
Keep moving , If the repayment is effective every time , be c
Must be reduced to 0
, here , You can collect the answers , When r - l
It's exactly the length of p
The length of the string , Then it must be stated in l
Position window , It has been judged , Next , Can be moved l
The pointer , Start shrinking the window . If l
The retracted position is exactly the effective position , be c++
( because l
The old position of no longer belongs to the current window ).
Time complexity O(N)
.
Complete code
public class LeetCode_0438_FindAllAnagramsInAString {
// The sliding window + Balance sheet
public List<Integer> findAnagrams(String s, String p) {
if (s == null || p == null) {
return new ArrayList<>();
}
List<Integer> ans = new ArrayList<>();
char[] str = s.toCharArray();
char[] pStr = p.toCharArray();
// word frequency list
int[] map = new int[26];
// Window from [l,r)
int l = 0;
int r = 0;
// Amount in arrears
int c = pStr.length;
for (char ch : pStr) {
map[ch - 'a']++;
}
int n = str.length;
while (r < n) {
if (map[str[r] - 'a'] > 0) {
// Effective repayment
c--;
}
map[str[r++] - 'a']--;
if (c == 0) {
// Collect to a location
ans.add(l);
}
// Form a window
if (r - l == pStr.length) {
// l Facing the window
if (map[str[l] - 'a'] >= 0) {
// Go back to
c++;
}
map[str[l++] - 'a']++;
}
}
return ans;
}
}
Similar problems
Sliding window maximum problem
Minimum covering substring problem
more
边栏推荐
- 第二章第二十二节:文件操作1
- Win10如何把图标发送到桌面
- &lt;3&gt;比较器Comparator的使用
- 黑马瑞吉外卖之过滤器后台登录验证(详细笔记说明)
- leetcode:1838. Frequency of the highest frequency element [sort + prefix and + dichotomy + thinking]
- 第二章 第二十五节:文件操作:with和复制
- Summer Challenge [FFH] NFC touches and pulls up any application without enterprise certification!
- 关于for...in和for...of理解和使用
- Use epoll to manage or golang multiprocess in case of a large number of connections
- Comment win10 envoie une icône au Bureau
猜你喜欢
第三章第四节:形参
黑马瑞吉外卖之过滤器后台登录验证(详细笔记说明)
第二章第二十节:运算符.1
Don't be ridiculous. Don't you know what abilities to improve if you want to enter a big factory? (collect quickly)
tsconfig.json在配置文件中找不到任何输入,怎么办?
网络爬虫爬取b站励志弹幕并生成词云(精心笔记总结)
First meet JS
第二章 第二十六节:文件操作:文件修改
Section 19 of Chapter 2: encoding and decoding
RHCSA 硬鏈接與軟鏈接的區別、一級目錄的解釋、重定向、創建文件及目錄、删除文件及目錄、cp命令的使用、mv命令的使用
随机推荐
Let me show you eight fallacies in software design
“万物互联,使能千行百业”,2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛即将开幕
C语言课程设计——宾馆管理系统
第二章第二十一节:运算符.2
跟我读论文丨Multi-Model Text Recognition Network
互联网寒冬,3个月如何从功能测试进阶自动化测试?【附学习指南】
Section 18 of Chapter 2: character set and coding
Section 3 of Chapter 3: parameter classification
本地数据如何高效灾备上腾讯云
leetcode:1838. Frequency of the highest frequency element [sort + prefix and + dichotomy + thinking]
Is it safe to open an account on flush? ETF trading rules and fees
【报错】ValueError: It seems that you are using the Keras 2 and you are passing both
将瑞吉外卖项目jar包部署在远程服务器并成功运行在pc和移动端
第二章 第十七节:字典知识补充
[300 opencv routines] 234 Principal component analysis (PCA) for feature extraction
第二章 第二十五节:文件操作:with和复制
Comment win10 envoie une icône au Bureau
Section 16 of Chapter 2: Circular nesting of dictionaries
第二章 第二十四节:文件操作:写
In the cold winter of the Internet, how can we advance from functional testing to automated testing in three months? [attached learning guide]