当前位置:网站首页>567. Arrangement of strings
567. Arrangement of strings
2022-07-23 09:51:00 【Xiao Lu wants to brush the questions】
List of articles
567. Arrangement of strings
Here are two strings s1 and s2 , Write a function to judge s2 Does it include s1 Permutation . If it is , return true ; otherwise , return false .
let me put it another way ,s1 One of the permutations is s2 Of Substring .
Example 1:
Input :s1 = “ab” s2 = “eidbaooo”
Output :true
explain :s2 contain s1 One of the permutations of (“ba”).
source : Power button (LeetCode)
link :https://leetcode.cn/problems/permutation-in-string
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking
The sliding window 
Balance sheet
Maintain a length of 5 The window of

I owe you two C. Now my window contains a C, Is a valid repayment ,C Must be reduced 
An effective repayment 
Negative value after subtraction , Not an effective repayment all Immobility 
For the first time, the window grows to 5. The window is initially formed
Is this window str2 Permutation
all It's not equal to 0, This window is not str2 Permutation 
Still not an effective arrangement
When the window slides
Add a character to the right , If the character exists in the debt table , This character is subtracted from the number in the table
Throw away a character on the left , If this character is in the debt table , This character is added to the number of the table
If the number of a character is reduced to 0, be all–
If a character is added 1, be all++
When all==0 when , Return to the answer
Code
This code returns the starting position of any substring that meets the condition
public static int containExactly(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() < s2.length()) {
return -1;
}
char[] str2 = s2.toCharArray();
int M = str2.length;
int[] count = new int[256];
for (int i = 0; i < M; i++) {
count[str2[i]]++;
}
int all = M;
char[] str1 = s1.toCharArray();
int R = 0;
// 0~M-1
for (; R < M; R++) {
// One of the earliest M Characters , Let its window take shape
if (count[str1[R]]-- > 0) {
all--;
}
}
// The window has initially formed , No judgment is valid or invalid , Decide the next position and judge
// The next process , Enter one right in the window , Spit one on the left
for (; R < str1.length; R++) {
if (all == 0) {
// R-1
return R - M;
}
if (count[str1[R]]-- > 0) {
all--;
}
if (count[str1[R - M]]++ >= 0) {
all++;
}
}
return all == 0 ? R - M : -1;
}
边栏推荐
- Judge whether it is void type
- 31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...
- 肽核酸偶联多肽Ile-Glu-Gly-Arg-pNA (S-2222)|Boc-Leu-Gly-Arg-PNA
- 【无标题】
- LEADTOOLS 20-22 Crack-DotNetCore!!!
- [secret history of bug] uint8 data exceeds the type range, output 0x0102
- PNA肽核酸修饰多肽Z-Gly-Pro-pNA|D-Phe-Pip-Arg-pNA|Tos-Gly-Pro-Arg-pNA
- Adas test plan
- PNA肽核酸修饰多肽Suc-Ala-Ala-pNA|2-Ala-Ala-LeupNA|Ac-Phe-Gly-PNA
- Teach you how to set up Alibaba cloud DDNS in Qunhui
猜你喜欢
随机推荐
Q-Vision+Kvaser CAN/CAN FD/LIN总线解决方案
Judge whether the two types are the same
【Node中间层实践(四)】----express中间层的逻辑处理
大专学历想0基础学编程以后找工作可行吗 ?
想放弃软件测试了,4年经验去面试10分钟结束,测试现在这么难了?
亿级流量下的分布式锁优化方案!太好用了~
Tidb 3.0安装
VirtualBox NAT network mode configuration
PNA PNA modified polypeptide bz- (DL) - Arg PNA | z-ala-ala-leu-pna | suc ala ala ala PNA
Blog milestones
PNA modified polypeptide BZ Val Gly Arg PNA | BOC Val Leu Gly Arg PNA
本地提权的学习
canal 03 (共8章)
零基础怎么学习单片机?
【学习笔记】Node--从0基础到实战企业官网
canal 配置01
Read-committed has no interval lock
qml-使用 listView 构筑三级树形(treeView)架构
中信期货是正规的期货公司吗,开户是否安全?
[SSM]异常处理









