当前位置:网站首页>LeetCode 567、字符串的排列
LeetCode 567、字符串的排列
2022-04-21 15:31:00 【亡于灬】
567、字符串的排列
1)题目描述
给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。
换句话说,s1 的排列之一是 s2 的 子串 。
示例 1:
输入:s1 = "ab" s2 = "eidbaooo"
输出:true
解释:s2 包含 s1 的排列之一 ("ba").
示例 2:
输入:s1= "ab" s2 = "eidboaoo"
输出:false
提示:
1 <= s1.length, s2.length <= 10^4s1和s2仅包含小写字母
2)分析
滑窗,哈希。使用两个表,一个用来记录s1中各个字符出现的次数,另一个用来记录s2中与s1等长子串中各个字符出现的次数。比较两个哈希表是否相等,若相等返回true,不相等则s2子串向后滑动一个字符,再比较。直到s2尾部结束。若结束还未出现相等,返回false。
3)C++代码
class Solution {
public:
bool checkInclusion(string s1, string s2) {
if(s1.length()>s2.length())
return false;
vector<int> hsahS1(26,0),hashS2(26,0);
for(int i=0;i<s1.length();i++){
hsahS1[s1[i]-'a']++;
hashS2[s2[i]-'a']++;
}
if(hsahS1==hashS2)
return true;
for(int i=s1.length();i<s2.length();i++){
hashS2[s2[i]-'a']++;
hashS2[s2[i-s1.length()]-'a']--;
if(hsahS1==hashS2)
return true;
}
return false;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124315896
边栏推荐
- Page 92 group informatization planning scheme enterprise application integration solution
- JDBC和数据库连接池
- Back to basics, multi-party security computing should return to the original consideration of "security"
- shell 知:awk
- Simple explanation of impermanence loss
- Best practices | under the epidemic, learn how eolink can help telecommuting!
- [binary search - simple] LCP 28 Procurement scheme
- 终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享
- LaTeX常用公式查询
- Leetcode 654: maximum binary tree
猜你喜欢

Oracle official announcement: Tencent JDK 18 ranks first in China!

Red team technology - parent process camouflage (mitre att & CK framework: t1134)

全国降雨侵蚀力因子R值

什么是电子邮件的格式?企业邮箱地址是什么?

Reading breaks ten thousand "volumes": National Reading insight 2022

LeetCode刷题之652寻找重复的子树

How to apply for corporate email? How to register email with mobile phone number?

51页数字转型与“十四五”信息化规划

万有导航:简洁实用的综合导航网站

Leetcode 654: maximum binary tree
随机推荐
Deltix Round, Summer 2021 D. Take a Guess(二进制计算常用性质汇总)
跟着中富金石学炒股,25年行业沉淀,投资要用实力说话
[binary search - medium] sword finger offer II 070 Sort numbers that appear only once in the array
返璞归真,多方安全计算要回归到“安全”的本源考虑
Smart public security QR code positioning alarm system development of mobile police app
How to apply for corporate email? How to register email with mobile phone number?
Obsidian automatically uploads pictures to the drawing bed - install picgo plug-in and configure
AcWing 1854. Promotion count (Analog)
shell 知:awk
107页企业数字化转型规划设计
What is an email address? Easy to use email registration application
百度云盘好用还是阿里云盘好用?
Deltix Round, Summer 2021 E. Equilibrium
终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享
EL&JSTL
How to synchronize client email to webmail and how to register email address?
Reading breaks ten thousand "volumes": National Reading insight 2022
MySQL-IF-THEN-ELSE语句
What conditions need to be met for an app to go online in the app store?
Chapter 7 class