【leetcode】438. Find All Anagrams in a String
problem
438. Find All Anagrams in a String
solution1:
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if(s.empty()) return {};
vector<int> res, pv(, );
for(auto a:p) pv[a]++;
int sn = s.size();
int i = ;
while(i<sn)
{
vector<int> tmp = pv;
bool is = true;
for(int j=i; j<i+p.size(); j++)
{
if(--tmp[s[j]]<)
{
is = false;
break;
}
}
if(is) res.push_back(i);
i++;
}
return res;
}
};
solution2:使用哈希表表示一定字符长度内各个字符的个数,每次滑窗需要添加最新的字符,且减去最旧的字符,然后比较哈希表。
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if(s.empty()) return{};
vector<int> res, m1(, ), m2(, );
for(int i=; i<p.size(); i++)
{
m1[p[i]]++;
m2[s[i]]++;
}
if(m1==m2) res.push_back();
for(int i=p.size(); i<s.size(); i++)
{
m2[s[i]]++;
m2[s[i-p.size()]]--;
if(m1==m2) res.push_back(i-p.size()+);
}
return res;
}
};
参考
1. Leetcode_438. Find All Anagrams in a String;
完
【leetcode】438. Find All Anagrams in a String的更多相关文章
- 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode❤python】 438. Find All Anagrams in a String
class Solution(object): def findAnagrams(self, s, p): """ :type s: s ...
- 【LeetCode】1417. 重新格式化字符串 Reformat The String
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode- ...
- 【LeetCode】1408. 数组中的字符串匹配 String Matching in an Array
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetco ...
- 【LeetCode】387. First Unique Character in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
随机推荐
- ldap 集成harbor
harbor: 1.6 默认配置文件在harbor.cfg,我们可以先不添加配置,直接在harbor web界面进行配置(harbor 1.6 如果db 启动失败提示postgresql 数据目录已存 ...
- vue style background
vue 动态加载背景图 :style="{backgroundImage: 'url('+ item.imgList[0] +')',backgroundRepeat:'no-repeat' ...
- Bugku-CTF之你必须让他停下+头等舱
Day8 你必须让他停下 地址:http://123.206.87.240:8002/web12/ 访问之后发现:
- 对负载均衡的理解及nginx负载均衡的配置
https://blog.csdn.net/qq_28602957/article/details/61615876
- luogu[愚人节题目3]现代妖怪殖民地 NTT
U34272 [愚人节题目3]现代妖怪殖民地 fft 题目链接 https://www.luogu.org/problemnew/show/U34272 思路 虽然是个py题. ntt(或者fft)模 ...
- (转载)Unity3D开发之编辑器统一修改Text字体
最近遇到一个需求,就是我们在做完一个场景后,美工感觉字体不好看,效果不是很好,想要换一种字体.UGUI的界面已经搭完,如果要一个一个Text寻找,工作量将是巨大.而且作为程序人员是不会容忍自己做这些机 ...
- 线上问题排查(2)——JDK内置工具
https://www.cnblogs.com/keanuyaoo/p/3253663.html 常用命令目录: jps命令(Java Virtual Machine Process Status T ...
- Windows环境——MySQL安装及配置
Mysql安装 下载地址:https://dev.mysql.com/downloads/mysql/ 根据个人需求,选择对应的操作系统,进行安装,本次安装的版本为5.7.24版本. 1. 安装完成 ...
- 51Nod - 1384 正常解法
这个是正常解法 #include<stdio.h> #include<string.h> #include<math.h> #include<time.h&g ...
- Linux 独立启动方式安装 Archiva
为了方便起见,我们假设你的 archiva 安装到目录 /opt 下面. 下载安装程序 进入 Archiva 的项目的下载页面中,请单击链接:https://archiva.apache.org/do ...