[leetcode-567-Permutation in String]
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1.
In other words, one of the first string's permutations is the substring of the second string.
Example 1:
Input:s1 = "ab" s2 = "eidbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input:s1= "ab" s2 = "eidboaoo"
Output: False
Note:
The input strings only contain lower case letters.
The length of both given strings is in range [1, 10,000].
思路1:
首先s1全排列,每一个全排列 查看是否是s2的子串。果然...超时,复杂度太高。
bool checkInclusion(string s1, string s2)
{
if(s1.size()>s2.size())return false;
char* s1char = new char(s1.size()+);
s1.copy(s1char,s1.size(),);
char* s1char2= new char(s1.size()+);
s1.copy(s1char2,s1.size(),);
do
{
if(s2.find(s1char) != string::npos) return true;
}while(next_permutation(s1char,s1char+strlen(s1char)));
do
{
if(s2.find(s1char2) != string::npos) return true;
}while(prev_permutation(s1char2,s1char2+strlen(s1char2)));
return false;
}
思路2:
学习了已经AC的大神的代码,发现他们无一例外都是用hash表格来做的。
思路就是:观察字符串s1和s2,如果s1的全排列是s2的子串,则s1的所有字符均出现在s2中,而且这些字符连续。
于是用一个类似于滑动窗口的区间去统计此区间内的字符,区间的长度取为s1的长度。
如果在s2中存在一个区间,里面的字符与s1的字符全部都一样,则说明s1通过全排列后一定能够得到s2中的子串。也即满足题意。
bool checkInclusion2(string s1, string s2)
{
int m = s1.length(),n = s2.length();
if(m>n)return false;
vector<int>maps1(),maps2();
for(int i=;i<m;i++)
{
maps1[s1[i] - 'a'] ++;
maps2[s2[i] - 'a'] ++;
}
if(maps1 == maps2) return true; //统计前m个字符是否符合条件
for(int i = ;i+m<n;i++)
{
maps2[s2[i] - 'a'] --;
maps2[s2[i+m] - 'a'] ++;
if(maps1 == maps2) return true;
}
return false;
}
[leetcode-567-Permutation in String]的更多相关文章
- [LeetCode] 567. Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- 567. Permutation in String
Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- 567. Permutation in String字符串的排列(效率待提高)
网址:https://leetcode.com/problems/permutation-in-string/ 参考:https://leetcode.com/problems/permutation ...
- 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation
[抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...
- 567. Permutation in String【滑动窗口】
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- 浅论Javascript在汽车信号测试中的应用
起因 上周老板又给了我这个车辆工程毕业的码农一份工作: 要我写一个测试台架出来. 我先简单的分析了测试台架的几种典型的工况: 1.发送一个CAN信号,测试能否查到. 2.发送一个信号,是否能在规定时间 ...
- 区划代码 node 版爬虫尝试
前言 对于区划代码数据,很多人都不会陌生,大多公司数据库都会维护一份区划代码,包含省市区等数据.区划信息跟用户信息息息相关,往往由于历史原因很多数据都是比较老的数据,且不会轻易更改.网上也有很多人提供 ...
- clojure 使用阿里云仓库
clojure 使用阿里云仓库 学习一门语言,如果没有梯子真的是一件非常痛苦的事情,好在阿里巴巴为我们java程序员提供了maven镜像.近期学习clojure,为了找解决方案在stackoverfl ...
- 关于html表格单元格宽度的计算规则
* { margin: 0; padding: 0 } body { background: #fafafa } ul,li { list-style: none } h1 { margin: 20p ...
- html学习笔记 - 标签
单标签 : <!DOCTYPE html> 解析类型标签 <!-- xxx --> 注释标签 <br /> 换行标签 <hr /> 分割线标签 < ...
- 关于JS的return false
之前真的不知道JS里的return false 还能跳出事件. 今天在修改BUG的时候,用到了这个,就去查了一下,为了加深记忆在此处做个总结. retrun true: 返回正确的处理结果. retu ...
- 前端小课堂 js:函数的创建方式及区别
js 函数的创建大体有这几种方式: -1-函数表达式(函数字面量): 说白了就是把一个函数赋值给了一个变量. var fun1 = function(index){ alert(index); } f ...
- Pandas日期数据处理:如何按日期筛选、显示及统计数据
前言 pandas有着强大的日期数据处理功能,本期我们来了解下pandas处理日期数据的一些基本功能,主要包括以下三个方面: 按日期筛选数据 按日期显示数据 按日期统计数据 运行环境为 windows ...
- SmartCoder每日站立会议09
站立会议内容 今天约了在一起编程,详细确定各个页面以及消息的添加.发送等一些小的细节. 1.站立会议照片: 2.任务展板 2.燃尽图
- Python多线程和多进程谁更快?
python多进程和多线程谁更快 python3.6 threading和multiprocessing 四核+三星250G-850-SSD 自从用多进程和多线程进行编程,一致没搞懂到底谁更快.网上很 ...