当前位置:网站首页>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;
};
边栏推荐
- IT自媒体高调炫富,被黑客组织盯上,铁定要吃牢饭了…
- STM32 Development Notes 119: what macros are required to enable FPU?
- 搭建私有CA服务器
- [live review] AI customer service "changes according to the situation", and man-machine dialogue can be easier
- Leetcode55. Jumping game
- 很多时候都是概率
- If you don't know these 20 classic redis interview questions, don't go to the interview!
- An article takes you to understand the sentinel mode of redis
- Baklib: share some methods about building enterprise knowledge management (km)
- 市场是对的
猜你喜欢

Ffmpeg download and installation

I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.

Implementation of recommendation system collaborative filtering in spark

Introduction to CpG control network

Solve the problem that uni app applet obtains routes and route parameters

epoll的实现原理

Completed project series Tutorials - smart campus management system

Ownership in rust -- introduction of rust language Xiaobai 11
搭建私有CA服务器

After watching the latest interview with big manufacturers, these six JVM interview questions were asked
随机推荐
Analysis of lottery winning numbers in history
Data link layer protocol -- Ethernet protocol
How to test data in the process of data warehouse migration?
Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
Implementation principle of epoll
Set up private CA server
85 distributed project construction
four hundred and forty-four thousand one hundred and forty-one
[no title] 1
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
2、 Mysql database foundation
Openworm project compilation
AUTOSAR from getting started to mastering 100 lectures (105) - protection mechanism of AUTOSAR timing for functional safety
深入掌握Service
OA and fansoft Bi cross system users, departments and posts synchronous summary
Unity LOD
Etcd learning
搭建私有CA服务器
This low code reporting tool is needed for data analysis
Summary of UPR optimization suggestions of unity