当前位置:网站首页>Sword finger offer II 015 All modifiers in the string
Sword finger offer II 015 All modifiers in the string
2022-07-22 06:12:00 【Code of attack】
The finger of the sword Offer II 015. All modifiers in the string
List of articles
One 、 subject
Original link : The finger of the sword Offer II 015. All modifiers in the string
1. Title Description
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 .
Example 1:
Input : s = "cbaebabacd", p = "abc"
Output : [0,6]
explain :
Start index equals 0 The substring is "cba", It is "abc" An inflection of .
Start index equals 6 The substring is "bac", It is "abc" An inflection of .
Example 2:
Input : s = "abab", p = "ab"
Output : [0,1,2]
explain :
Start index equals 0 The substring is "ab", It is "ab" An inflection of .
Start index equals 1 The substring is "ba", It is "ab" An inflection of .
Start index equals 2 The substring is "ab", It is "ab" An inflection of .
Tips :
1 <= s.length, p.length <= 3 * 104
s
andp
Only lowercase letters
Be careful : This topic and the main station 438 The question is the same : https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/
2. Basic framework
C++ The basic framework code is as follows :
vector<int> findAnagrams(string s, string p) {
}
3. Their thinking
- Topic analysis
- The goal of the topic is to return the starting subscript of all the modified word substrings .
- An anagram string is a string in which the number of elements is the same and the type is the same , Strings in different order .
- Define two sets of hash arrays
s_rec
andq_rec
, amongs_rec
The function of array as a sliding window record element . If the current sliding window arrays_rec
be equal toq_rec
Array , Then it means that the substring in the current sliding window is Anagram string . - Move the sliding window to the right every time , Just move out the element on the left side of the window , Move a new element to the right of the window . The corresponding operation is that the elements on the left side of the window are
s_rec
Subtract one from the position of the index in , The new element on the right iss_rec
Add one to the position of the index in . - If you find an anagram string , Then the leftmost element of the sliding window is taken as the starting position of the substring , Store in
res
Array , When the traversal is over , returnres
Array ,res
The array representss
The starting position of all modifier substrings in the string .
Implementation code :
vector<int> findAnagrams(string s, string p) { if (s.length() < p.length()) return { }; // There is no modifier string vector<int> s_rec(26, 0); vector<int> p_rec(26, 0); vector<int> res; int len = p.size(); int i; for (i = 0; i < p.size(); i++) { ++s_rec[s[i] - 'a']; ++p_rec[p[i] - 'a']; } if (s_rec == p_rec) res.push_back(0); // With 0 For the starting node s Whether the substring is an anagram string for (i = 1; i < s.size() - len + 1; i++) { --s_rec[s[i - 1] - 'a']; ++s_rec[s[i + len - 1] - 'a']; if (s_rec == p_rec) // Whether the current sliding window is an anagram string res.push_back(i); } return res; }
Two 、 Summary of knowledge points
This time we use h a s h hash hash and The sliding window , Every time you slide a window, you subtract one element from the left , Add an element on the right , It is no longer necessary to re count the middle elements in the window , Thus reducing the number of operations .
边栏推荐
- 网络连接工具大全
- 互联网和传输层协议
- 百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
- EF Core 数据过滤
- Discussion on passing the d-shield PHP webshell without killing horses
- 一次 MySQL 误操作导致P0级事故
- Noipd2t2 - treasure solution
- Control of semiconductor refrigeration and heating based on general single chip microcomputer control of cold and hot head in mobile phone radiator massage instrument
- NISP-2之信息安全技术系列博文目录
- Internet and transport layer protocol
猜你喜欢
An idea of solving agile iteration of desktop application with applet Technology
一些工具改造
Control of semiconductor refrigeration and heating based on general single chip microcomputer control of cold and hot head in mobile phone radiator massage instrument
92. (leaflet chapter) leaflet situation plotting - acquisition of attack direction
[MATLAB]:基础知识学习
iNFTnews | 佳士得推出风险投资部门,瞄准Web3和元宇宙产业
Postman - post请求application/x-www-from-urlencoded
leetcode 931.下降路径最小和
Just one dependency to give swagger a new skin, which is simple and cool
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
随机推荐
D3.js + Canvas 绘制组织结构图
Discussion on passing D shield JSP webshell+ ice scorpion avoiding killing horses
Internet and transport layer protocol
Development of digital collection system -- Construction of mall blind box H5 platform
6.1 恶意代码防范
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
Wechat applet_ 19. Custom components -behaviors
Feynman learning method
What is the culprit of the failure of digital transformation?
07.Octave语言的使用-变量、数值、向量、矩阵
C multithreading and asynchronism (II) -- detailed explanation of task and async/await
Redis: master-slave replication
DLL免杀技术探讨
D3.js + canvas drawing organization chart
Encryption and decryption of yii2
Relevant use cases of QT events
FileReader
Can or could
数据中心线缆管理
Selenium常用实战功能指南