当前位置:网站首页>Sword finger offer II 014. anagrams in strings
Sword finger offer II 014. anagrams in strings
2022-07-25 05:06:00 【rananie】
List of articles
The finger of the sword Offer II 014. An anamorphic word in a string
subject
Given two strings s1 and s2, Write a function to judge s2 Does it include s1 An anamorphic word of .
let me put it another way , One of the permutations of the first string is the... Of the second string Substring .
Example 1:
Input : s1 = "ab" s2 = "eidbaooo"
Output : True
explain : s2 contain s1 One of the permutations of ("ba").
Example 2:
Input : s1= "ab" s2 = "eidboaoo"
Output : False
Tips :
1 <= s1.length, s2.length <= 104
s1 and s2 Only lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/MPnaiL
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
Concerns
1.s1 Which characters have appeared in
2.s1 After random combination of characters , stay s2 Need to appear continuously in
We're traversing s1 When , Can be s1 The characters that have appeared are stored in map in .key Character ,value Is the number of occurrences . All lowercase letters here can also be used 26 Bit array implementation .
Traverse s2, Assume that the coordinates are i, So at this time [i,i+s1.length-1] It should all be in s1 There have been . This will ensure continuity .
How to compare [i,i+s1.length-1] and s1 Whether the array of is the same ? You can give s2 Also create an array , Record the elements and number that have appeared in the window .
var checkInclusion = function(s1, s2) {
let s1Arr=new Array(26).fill(0);
let s2Arr=new Array(26).fill(0);
if(s2.length<s1.length)return false;
for(let ch of s1){
// Record s1 The situation of
s1Arr[ch.charCodeAt()-'a'.charCodeAt()]++;
}
let left = 0;
for(let right = 0; right < s2.length; right ++) {
s2Arr[s2[right].charCodeAt() - 'a'.charCodeAt(0)] ++;
if(right - left + 1 === s1.length) {
// When the window size is just right , Compare whether the elements inside are equal
if(s1Arr.toString() === s2Arr.toString()) return true
}
if(right >= s1.length - 1) {
// If the window size exceeds , It means you need to start shrinking
s2Arr[s2[left].charCodeAt() - 'a'.charCodeAt()]--;
left ++;
}
}
return false;
};
边栏推荐
- ESWC 2018 | r-gcn: relational data modeling based on graph convolution network
- Purpose of setting novice task period in the integral system
- Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
- An article takes you to understand the sentinel mode of redis
- Introduction to fundamentals of operations research [1]
- Docker builds MySQL master-slave replication
- STM32 Development Notes 119: what macros are required to enable FPU?
- 小说抓取实战
- Why does the official not recommend using UUID as MySQL primary key
- Ffmpeg download and installation
猜你喜欢

Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
![Introduction to fundamentals of operations research [1]](/img/79/b613bff74a78ad63f202b9e652220d.png)
Introduction to fundamentals of operations research [1]

Document collaboration tool recommendation

After watching the latest interview with big manufacturers, these six JVM interview questions were asked

HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态

Actual combat | record an attack and defense drill management

2022-7-18 summary

中创算力荣获「2022年科技型中小企业」认定

推荐系统-协同过滤在Spark中的实现
![[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)](/img/01/42de6280191b9c32a7f37d7727bd4f.png)
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
随机推荐
[untitled]
Pychart configuration pyqt5
Why does the official not recommend using UUID as MySQL primary key
When we talk about immutable infrastructure, what are we talking about
GDT,LDT,GDTR,LDTR
Bypass XSS filters in Web Applications
Pikachu vulnerability platform exercise
[globally unique ID] how to handle the ID primary key after dividing the database and table?
Completed project series Tutorials - smart campus management system
Wechat official account all article download links to get
Natural state is the best
[sht30 temperature and humidity display based on STM32F103]
[no title] 1
The strongest JVM in the whole network is coming!
Baklib: share some methods about building enterprise knowledge management (km)
[untitled]
Go language function
Data Lake (16): structured streaming writes iceberg in real time
Unity LOD
Market regulation