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. [刷题]算法竞赛入门经典(第2版) 4-8/UVa12108 - Extraordinarily Tired Students

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa12108 - Extraordinarily Tired Stude ...

  2. 我必须得告诉大家的MySQL优化原理

    本文转载自http://www.jianshu.com/p/d7665192aaaf 说起MySQL的查询优化,相信大家积累一堆技巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段 ...

  3. 无锁atomicInteger

    AtomicInteger可以保证硬件上的原子操作 1.主要原理 CAS操作 在进行数据更新的时候,会进行与内存中的地址进行比较,若预期值与内存中的值相同,则进行数据上的更新,若值不同,则更新失败,  ...

  4. rpm不用yum安装rabbitMQ

    1.安装erlang 下载 esl-erlang_19.0~centos~6_amd64.rpm 打开https://www.erlang-solutions.com/resources/downlo ...

  5. (转)Amoeba for MySQL 非常好用的mysql集群软件

    Amoeba for MySQL Amoeba for MySQL致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当query 路由功能,专注 分布式数据库 proxy ...

  6. 010一对一 主键关联映射_双向(one-to-one)

    ²  两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²  有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...

  7. 每天一道Java题[10]

    题目 阐述创建线程最常用的两种方法及其对比. 解答 方法一:继承Thread类实现 步骤: 创建Thread类的子类,如MyThread. 重写Thread类的run()方法. 实例化MyThread ...

  8. git reset、git checkout和git revert的区别

    这三个git命令都是用来撤销代码仓库中的某些更改,而前两个命令不仅可以作用于commit层面,还可以作用于file层面Reset在commit层面,reset通过移除当前分支的一些节点来实现版本回滚; ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...

  10. Java 9 揭秘(1.Java入门介绍)

    文 by / 林本托 在第一部分中,主要讲解如下内容: JDK 9 包含了哪些内容 运行代码的系统要求 如何安装 NetBeans 1 JDK 介绍 JDK 9是Java开发工具包的第九个主要版本,计 ...