当前位置:网站首页>Sword finger offer II 014 Anagrams in strings
Sword finger offer II 014 Anagrams in strings
2022-06-10 00:49:00 【Small white yards fly up】
subject
link :https://leetcode.cn/problems/MPnaiL/
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 .

Ideas
The description of the topic is not clear , What I understand is : All the characters in the first string are arranged arbitrarily , Can form a substring in the second string .
Then the condition we have to meet is :
- s2 The length of the qualified substring in is equal to s1 The length of
- s2 The number of occurrences of each lowercase letter in this substring , be equal to s1 The number of occurrences of each lowercase letter in
Continuous substring , It's easy to think of double pointers . And count the number of lowercase letters , Again, we are familiar with , Length can be used 26 Bit array ( Hashtable ), Save the number of occurrences of each letter .
solution : Double pointer + Hashtable
Logic
- Use a length 26 An array of bits holds the number of occurrences of each lowercase letter
- Traverse s1, Fill the array
- Double pointer processing s2, First from 0 Start , Move the right pointer to the right s1.length-1 The location of . Every time I move , Look at the current subscript letter , And subtract... From the corresponding position in the array 1.
- Both hands move to the right at the same time . Letter moved out by left pointer , Add... To the corresponding position of the array 1, The letter in which the right pointer moves , Subtract... From the corresponding position of the array 1. Each move counts whether the array is 0, Yes, go back to true.
Code
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
int[] counts = new int[26];
for (int i = 0; i < s1.length(); i++) {
counts[s1.charAt(i) - 'a'] += 1;
counts[s2.charAt(i) - 'a'] -= 1;
}
if (allZero(counts)) {
return true;
}
for (int i = s1.length(); i < s2.length(); i++) {
counts[s2.charAt(i) - 'a'] -= 1;
counts[s2.charAt(i-s1.length()) - 'a'] += 1;
if (allZero(counts)) {
return true;
}
}
return false;
}
public boolean allZero(int[] counts){
for (int count : counts) {
if (count != 0) {
return false;
}
}
return true;
}
边栏推荐
- Apply the latest ad and Txk patches
- Aquanee will land in gate and bitmart in the near future, providing a good opportunity for low-level layout
- 剑指 Offer II 014. 字符串中的变位词
- How WPS merges cells with different sizes
- Deep and shallow copy of mat data
- 试题 历届试题 子串分值和【第十一届】【省赛】【B组】
- Mat数据的深浅拷贝
- 线性规划和对偶规划学习总结
- mpls vpn
- Transformer
猜你喜欢
随机推荐
Collection backup | summary of some common problems about oauth2
Fix Microsoft app store flash back for win10
BGP协议实验
重发布实验
Solution to the C language problem of the sum of two numbers
RHCSA第二天
力扣 两数之和 C语言 题解
wps文本框如何设置透明
OSPF experiment
采云端&采云链:从订单协同到采购供应链,让采购供应链互联互通
The binary tree is expanded into a linked list [the essence of tree processing -- how to handle each sub tree]
Minimum toll
Code case - web version confession wall and file upload
Republish experiment
Facial Emotion Recognition: State of the Art Performance on FER2013
How to set transparency for WPS text box
[typecho]some problems in SQL programming
Disorder of flinksql
AQUANEE将在近期登陆Gate以及BitMart,低位布局的良机
Palindromes of past real questions of test questions date [11th session] [provincial competition] [group B]





