当前位置:网站首页>力扣练习——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;
}
边栏推荐
- HCIP ---- VLAN
- Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
- 如何使用工程仪器设备在线监测管理系统
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- 动作捕捉系统用于室内组合定位技术研究
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- CodeChef STMRRG String Merging (dp)
- Three-phase 380V rectified voltage
- 技能大赛训练题:组策略一
猜你喜欢

【勇敢饭饭,不怕刷题之链表】链表反转的几种情况

关于振弦采集模块及采集仪振弦频率值准确率的问题

什么是幂等性?四种接口幂等性方案详解!

中小规模网站架构

推荐6个自媒体领域,轻松易上手

Dry goods!ASSANet: Making PointNet++ faster and stronger
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create

Emulate stm32 directly with proteus - the programmer can be completely discarded
![[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists](/img/06/9d49fc99ab684f03740deb2abc38e2.png)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
随机推荐
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
OneFlow源码解析:算子指令在虚拟机中的执行
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
越折腾越好用的 3 款开源 APP
HDU 1520 Anniversary party (tree dp)
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
关于振弦采集模块及采集仪振弦频率值准确率的问题
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
SQL与NoSQL最终会走向融合吗?
Will SQL and NoSQL eventually converge?
如何使用工程仪器设备在线监测管理系统
HCIP ---- VLAN
从产品维度来看 我们为什么不能完全信任Layer2?
C#实战:基于ItextSharp技术标签生成小工具
The usage and difference between getParameter() and getAttribute()
TCP/IP笔记
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
The impact of development mode on testing
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
企业如何判断数据治理是否成功?