当前位置:网站首页>Licking Exercise - 63 Find all anagrams in a string
Licking Exercise - 63 Find all anagrams in a string
2022-08-10 11:34: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.输出说明
Output a series of integers,is the starting index of the substring,按照由小到大的顺序输出,整数之间以空格分隔.
If the starting index set is empty,则输出“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);//记录sThe number of occurrences of each character in
vector<int>pCount(26);//记录pThe number of occurrences of each character in
for (int i = 0; i < p_len; i++)//Iterates over a string each timep长度
{
sCount[s[i] - 'a']++;
pCount[p[i] - 'a']++;
}
//Because the title indicates that the string is composed of lowercase letters,Therefore, the comparison of characters can be converted into comparison of numbers
if (sCount == pCount)//数组相等
res.emplace_back(0);//The matching subscript is0
//滑动窗口
for (int i = 0; i < s_len - p_len; i++)//注意i的下标
{
sCount[s[i] - 'a']--;//Iterates over one character at a timech,需要把sCount中chThe number of corresponding subscript occurrences is decremented by one
sCount[s[i+p_len]-'a']++;//When the window slides to the next character,Increments the number of occurrences of the character by one
if (sCount == pCount)//A judgment is made every time the window is slid
{
res.emplace_back(i+1);//Because here is the new one obtained after the window is moved one square to the rightsCount和pCount,So if the conditions are met,resis inserted at 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;
}
边栏推荐
- 推荐6个自媒体领域,轻松易上手
- YTU 2894: G--我要去内蒙古大草原
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- Double.doubleToLongBits() method uses
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- 今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
- 软件架构简介
- Article take you understand interrupt the key driver of polling mechanism
- Where can I view the version record of WeChat applet submission review history?
- HDU 1520 Anniversary party (tree dp)
猜你喜欢
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
Store limited time seckill function system
SQL优化最强总结 (建议收藏~)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
Some tips for using Unsafe
模块九 - 设计电商秒杀系统
Mobile and PC compatible loading and toast message plugins
Short video software development - how to break the platform homogenization
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
随机推荐
Get started quickly and conquer three different distributed architecture calling schemes
Break through the dimensional barriers and let the dolls around you move on the screen!
关于振弦采集模块及采集仪振弦频率值准确率的问题
Three-phase 380V rectified voltage
The impact of development mode on testing
力扣练习——56 寻找右区间
LAXCUS分布式操作系统安全管理
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
【电商运营】你真的了解社交媒体营销(SMM)吗?
中小规模网站架构
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
HDU 1520 Anniversary party (tree dp)
学长告诉我,大厂MySQL都是通过SSH连接的
做自媒体月入几万?博主们都在用的几个自媒体工具
HDU 1520 Anniversary party (树型dp)
第2章-矩阵及其运算-矩阵运算(2)
老板加薪!看我做的WPF Loading!!!
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?