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]的更多相关文章

  1. [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 ...

  2. 567. Permutation in String

    Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...

  3. 【LeetCode】567. Permutation in String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...

  4. 567. Permutation in String字符串的排列(效率待提高)

    网址:https://leetcode.com/problems/permutation-in-string/ 参考:https://leetcode.com/problems/permutation ...

  5. 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation

    [抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...

  6. 567. Permutation in String【滑动窗口】

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  7. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  8. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  9. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  10. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. Linux安装yum以及更新yum版本

    .使用RedHat系统不能正常使用yum安装 由于RedHat没有注册,所有不能使用它自身的资源更新,于是使用yum instatll gcc-c++的时候出现This system is not r ...

  2. bootstrap实例 之 响应式表格-----2017-05-15

    Bootstrap 的响应式 CSS 能够自适应于台式机.平板电脑和手机,现在就bootstrap的响应式举一个例子: 如上图所示,要实现该表格在手机等移动端上只显示代号.名称.和价格,其他以查看详情 ...

  3. Linux学习第一步(虚拟机的和镜像文件的安装)

    一.安装虚拟机(本文以vmware workstation 12为例) 1.在网上所有虚拟机并下载. 2.找到下载文件安装好 3.一直下一步 4.接下来的就是选择安装的目录了,当然如果你的电脑c盘够大 ...

  4. Sqlla: 数据库操作从未如此简单

    Sqlla 一套数据库的 ORM 微型库,提供简单高效的 API 来操作数据库. Sqlla 拥有极少的API,使用方式简单.让开发者不需要关心数据库操作的具体细节,只需专注SQL和业务逻辑.同时简单 ...

  5. CSS3如何实现超出指定文本以省略号显示效果

    不做前端很久了,今天从重构师那里了解到CSS3已经可以实现很多以往必须通过JS才能实现的效果,如渐变,阴影,自动截断文本展示省略号等等强大效果,而且这些功能日渐成熟,已经大量用于生产环境.H5真的日渐 ...

  6. 5、Java Swing布局管理器(FlowLayout、BorderLayout、CardLayout、BoxLayout、GirdBagLayout 和 GirdLayout)

    5.Java-Swing常用布局管理器       应用布局管理器都属于相对布局,各组件位置可随界面大小而相应改变,不变的只是其相对位置,布局管理器比较难以控制,一般只在界面大小需要改是才用,但即使这 ...

  7. JQuery模拟实现天猫购物车动画效果

    测试程序源代码下载地址:源码 一.功能描述: 1.点击购买按钮,模拟抛物线将物品弹到购物车里: 2.购物车添加物品后,显示+1动画: 效果图如下: 实现如下: 1.导入jquery相关的包: < ...

  8. 【Netty】ChannelHandler和ChannelPipeline

    一.前言 前面学习了Netty的ByteBuf,接着学习ChannelHandler和ChannelPipeline. 二.ChannelHandler和ChannelPipeline 2.1 Cha ...

  9. bootstrap4中文版(纯手工翻译)

    1初步开始 1.1依赖 这个仓储包含一系列基于bootstrap标识和css样式的原生angular2指令.所以是不需要依赖jq和bootstrap.js的.只需要以下依赖即可: Angular(需要 ...

  10. 今天重装系统后,Wdows更新提示“windows update当前无法检查更新,因为未运行服务。您可能需要重新启动计算机”

    到百度搜了常用的解决方法,就是用命令提示符,但对我的情况不管用,提示“拒绝访问”.后来在08绿软站的一篇文章中找到了解决办法.原文如下(我本人也是用的第四种方法解决的): 试了下面几种解决方法,第四种 ...