当前位置:网站首页>Sword finger offer II 015 All modifiers in the string
Sword finger offer II 015 All modifiers in the string
2022-06-10 00:50:00 【Small white yards fly up】
Summary
Advance of the previous question , It's still a double pointer + Hash table solution , Just don't stop this time .
subject
link :https://leetcode.cn/problems/VabMRr
Given two strings s and p, find s All in p Of A modifier The string of , Returns the starting index of these substrings . Regardless of the order of the answer output .
A modifier Means the same letters , But arrange different strings .
Ideas
Advance of the previous question , Because you want to find multiple substrings . So on the basis of the previous question , Do not return directly when the first substring is found , Instead, record the starting index , Then the double pointer continues to move to the right .
solution : Double pointer + Hashtable
Code
public List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new ArrayList<>();
if (p.length() > s.length()) {
return result;
}
int[] counts = new int[26];
for (int i = 0; i < p.length(); i++) {
counts[p.charAt(i) - 'a'] += 1;
counts[s.charAt(i) - 'a'] -= 1;
}
if (allZero(counts)) {
result.add(0);
}
for (int i = p.length(); i < s.length(); i++) {
counts[s.charAt(i) - 'a'] -= 1;
counts[s.charAt(i-p.length()) - 'a'] += 1;
if (allZero(counts)) {
result.add(i - p.length() + 1);
}
}
return result;
}
public boolean allZero(int[] counts){
for (int count : counts) {
if (count != 0) {
return false;
}
}
return true;
}
边栏推荐
- IDC authority predicts that China's manufacturing industry will soon take advantage of the cloud
- Weights of complete binary tree of past real questions [10th] [provincial competition] [group B]
- typora 基本使用和更换typora的主题样式
- ospf总结
- OSPF summary
- BGP summary
- Record a malicious keyword brushed by illegal drainage
- 代码案例—— web版表白墙及文件上传
- 力扣 自除数 C语言 题解
- 线性规划和对偶规划学习总结
猜你喜欢

ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks

BGP protocol experiment

rip實驗

JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax

力扣 无重复字符的最长子串 C语言 题解

University of Ulm, Germany | comparative characterization of 3D protein structure

RHCSA第一天

If function selection when determining whether it is empty

采云端&采云链:从订单协同到采购供应链,让采购供应链互联互通

IDC fait autorité pour prédire que l'industrie manufacturière chinoise est sur le point de monter dans le nuage
随机推荐
剑指 Offer II 011. 0 和 1 个数相同的子数组
hcip第一次作业
Apply the latest ad and Txk patches
How to set transparency for WPS text box
How to keep the contents of WPS merged cells in one cell
BGP experiment
OSPF experiment
Republish experiment
Learning notes of zhouzhihua in machine learning
赛迪顾问《2021-2022年中国政务云市场研究年度报告》发布 华云数据跃居行业领军者
On cookies and sessions -- including cases and detailed notes
wps合并单元格后怎么将文字的位置进行调整
Pycharm 2022 permanently activated version download, valid for personal test
Hcip day 1 Summary
加解密及验签
OSPF实验
46 year old new academician: when I was a graduate student, I unloaded all the Games
flutter pub get failed (66; Could not find a file named “pubspec.yaml“
RHCSA第三天
BGP总结