当前位置:网站首页>力扣练习——63 找到字符串中所有字母异位词
力扣练习——63 找到字符串中所有字母异位词
2022-08-10 10:59:00 【qq_43403657】
63 找到字符串中所有字母异位词
1.问题描述
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。
说明:
字母异位词指字母相同,但排列不同的字符串。
示例 1:
输入:
s: “cbaebabacd” p: “abc”
输出:
[0, 6]
解释:
起始索引等于 0 的子串是 “cba”, 它是 “abc” 的字母异位词。
起始索引等于 6 的子串是 “bac”, 它是 “abc” 的字母异位词。
示例 2:
输入:
s: “abab” p: “ab”
输出:
[0, 1, 2]
解释:
起始索引等于 0 的子串是 “ab”, 它是 “ab” 的字母异位词。
起始索引等于 1 的子串是 “ba”, 它是 “ab” 的字母异位词。
起始索引等于 2 的子串是 “ab”, 它是 “ab” 的字母异位词。
2.输入说明
输入两个字符串s和p,只包含小写英文字母,长度都不超过 20100
3.输出说明
输出一系列整数,为子串的起始索引,按照由小到大的顺序输出,整数之间以空格分隔。
如果起始索引集合为空,则输出“none”,不包括引号。
4.范例
输入
cbaebabacd
abc
输出
0 6
5.代码
#include<iostream>
#include<map>
#include<string>
#include<unordered_map>
#include<algorithm>
#include<string.h>//memset函数
using namespace std;
vector<int> indexOfString(string s, string p)
{
int s_len = s.size();//字符串s长度
int p_len = p.size();//目标字符串长度
if (s_len < p_len)
return vector<int>();
vector<int>res;
vector<int>sCount(26);//记录s中每个字符出现次数
vector<int>pCount(26);//记录p中每个字符出现次数
for (int i = 0; i < p_len; i++)//每次都要遍历一个字符串p长度
{
sCount[s[i] - 'a']++;
pCount[p[i] - 'a']++;
}
//因为题目表示字符串都是由小写字母组成,所以可以把字符的比较转化为数字的比较
if (sCount == pCount)//数组相等
res.emplace_back(0);//匹配的下标为0
//滑动窗口
for (int i = 0; i < s_len - p_len; i++)//注意i的下标
{
sCount[s[i] - 'a']--;//每次遍历完一个字符ch,需要把sCount中ch对应的下标出现数量减一
sCount[s[i+p_len]-'a']++;//窗口滑动到下一个字符时,将字符出现的次数加一
if (sCount == pCount)//每次滑动窗口时要进行判断
{
res.emplace_back(i+1);//因为这里是窗口向右移动一格后得到的新的sCount和pCount,所以若满足条件,res中插入的位置为i+1
}
}
return res;
}
int main()
{
string s, p;
cin >> s >> p;
vector<int>res = indexOfString(s, p);
if (res.size() == 0)
cout << "none" << endl;
else
{
for (int i = 0; i < res.size()-1; i++)
{
cout << res[i] << " ";
}
cout << res[res.size() - 1] << endl;
}
return 0;
}
边栏推荐
- Emulate stm32 directly with proteus - the programmer can be completely discarded
- 高阶组件使用
- The brave rice rice, does not fear the brush list of 】 list has a ring
- Break through the dimensional barriers and let the dolls around you move on the screen!
- HCIP ---- VLAN
- 技能大赛训练题:组策略一
- OSSCore 开源解决方案介绍
- Store limited time seckill function system
- mysql5.7安装部署-yum安装
- 2022年裁员潮,失业程序员何去何从?
猜你喜欢

Redis (three) - detailed configuration file, publish and subscribe, new data types

让软件飞——“X+”技术揭秘

L2 applications from a product perspective: why is it a playground?

基于UiAutomator2+PageObject模式开展APP自动化测试实战

学长告诉我,大厂MySQL都是通过SSH连接的

Dry goods!ASSANet: Making PointNet++ faster and stronger

用proteus直接仿真stm32-可以完全丢弃编程器

【勇敢饭饭,不怕刷题之链表】链表倒数节点问题

商城限时秒杀功能系统

GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
随机推荐
CPU多级缓存与缓存一致性
Introduction to cross-end development of Taro applet
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
What is affecting MySQL performance?
快速上手,征服三种不同分布式架构调用方案
Codeforces 814 C. An impassioned circulation of affection (dp)
Short video software development - how to break the platform homogenization
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Emulate stm32 directly with proteus - the programmer can be completely discarded
Unsafe的一些使用技巧
Double.doubleToLongBits() method uses
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
mysql5.7 installation and deployment - yum installation
HDU 1520 Anniversary party (tree dp)
Cybersecurity Notes 5 - Digital Signatures
Codeforces 814 C. An impassioned circulation of affection (dp)
OneFlow源码解析:算子指令在虚拟机中的执行
第2章-矩阵及其运算-矩阵运算(2)
In August the DB list latest scores - database Engines