Problem statement:

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

Solution one: DFS permutation and string match.

This is the third question of leetcode weekly contest 30. I find an approach to solve this problem, it does not accept since of the TLE error.

Like 46. Permutations, I find all permutations and check the string if current permutation exists in the s2. It costs times.

To find all permutations, it costs O(n ^ n).

Since I did not save the code, and a long time after the computation, just brief describe the idea.

Solution two: sliding window. 

The problem asks the permutation, it contains two key information: (1) the order does not matter. (2) the letters are consecutive.

Basic above two characteristics, we can use a sliding window to solve this problem, which is different with 424. Longest Repeating Character Replacementand 594. Longest Harmonious Subsequence, the size of sliding window is fixed(the length of s1).

Suppose the length of s1 is k, s2 is n. The basic steps include:

  • Initialize the sliding window with first k elements. We should check if letters in current sliding window equal to s1.
  • Each time when we moved forward while erasing the element out of the sliding window.

Time complexity is O(k * n).

class Solution {
public:
bool checkInclusion(string s1, string s2) {
if(s1.size() > s2.size()){
return false;
}
int s1_len = s1.size();
int s2_len = s2.size();
vector<int> s1_cnt(, );
vector<int> s2_cnt(, );
for(int ix = ; ix < s1_len; ix++){
s1_cnt[s1.at(ix) - 'a']++;
}
for(int ix = ; ix < s2_len; ix++){
s2_cnt[s2.at(ix) - 'a']++;
if(ix >= s1_len){
s2_cnt[s2.at(ix - s1_len) - 'a']--;
}
if(s1_cnt == s2_cnt){
return true;
}
}
return false;
}
};

567. Permutation in String的更多相关文章

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

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

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

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

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

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

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

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

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

  6. [LeetCode] Permutation in String 字符串中的全排列

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

  7. [Swift]LeetCode567. 字符串的排列 | Permutation in String

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

  8. 60. Permutation Sequence (String; Math)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. LeetCode Permutation in String

    原题链接在这里:https://leetcode.com/problems/permutation-in-string/description/ 题目: Given two strings s1 an ...

随机推荐

  1. 四大开源协议比较:BSD、Apache、GPL、LGPL【转载】

    四大开源协议原文链接 本文参考文献:http://www.fsf.org/licensing/licenses/ 现今存在的开源协议很多,而经过Open Source Initiative组织通过批准 ...

  2. 179 Largest Number 把数组排成最大的数

    给定一个非负整数的列表,重新排列它们的顺序把他们组成一个最大的整数.例如,给定 [3, 30, 34, 5, 9],最大的组成数是 9534330.注意: 结果可能非常大,所以您需要返回一个字符串而不 ...

  3. android开发学习——This support library should not use a different version

    http://blog.csdn.net/mvpstevenlin/article/details/55209966 完美解决系列

  4. 《基于Node.js实现简易聊天室系列之项目前期工作》

    前期工作主要包括:项目的创建,web服务器的创建和数据库的连接. 项目创建 网上关于Node.js项目的创建的教程有很多,这里不必赘述.Demo所使用的Node.js的框架是express,版本为4. ...

  5. c# winform如何屏蔽键盘上下左右键

    重写事件: protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Up || keyData == ...

  6. iTOP-4412开发板全新升级支持4G全网通模块

    开发板支持4G,GPS,CAN,485,WIFI蓝牙,重力加速度计,陀螺仪等模块. 核心板参数 尺寸:6cm*7cm 高度:连同连接器在内0.26cm CPU:Exynos4412,四核Cortex- ...

  7. 【java_设计模式】建造者模式

    应用场景对生成对象进行链式配置好处:按需配置对象,灵活修改默认配置,比构造方法创建对象更直观. public static void init() {if (okHttpClient == null) ...

  8. 02Document Type Definition

    Document Type Definition 1. Document Type Definition DTD(Document Type Definition)文件格式定义作用是给予文件一种格(T ...

  9. idea vue提示

    我在idea中,装了,vue.js插件,可就是死活,没有提示,我的html 文件是 .html扩展名,我想是不是因为这个原因,我就新建了个,test.vue,哈哈,提示出来了,我又在其它.html文件 ...

  10. Perl语言入门:第七章习题:输出文件中包含一个大写字母的所有行,不输出一行的内容全是大写的

    文件内容: bash-2.03$ cat file_4_ex_ch7.txt anonymous attribute demolition grammar rules indices refernce ...