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. 转 ORA-00054 的解决方法

    统有一个不用的索引,想删除这个索引, SQL> drop index GPSTIME_GLOBAL_INDEX  2  /drop index GPSTIME_GLOBAL_INDEX      ...

  2. 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...

  3. joomla建站-双语CMS系统开发的实现

    首先,请确保你的网站安装了你所需的双语语言,详细安装过程见:https://www.cnblogs.com/surfer/p/9619345.html 第一步:设置内容管理 可以按照个人需求进行语言编 ...

  4. css3中content属性的应用

    可以使用css3中content功能为html元素增减内容.content需要配合 E:before和E:after使用. 废话少说,看代码和效果说明: 第一种: css代码: #div1:befor ...

  5. 微信小程序之多行文本省略号

    最近在捣鼓小程序,期间遇到的问题,踩过的坑,也是在网上各种搜.这里也说下我解决的问题,方便大家. 在小程序首页显示文本列表的时候,为了美观,不希望把所有的文本都显示出来,希望是显示前几行(比如前3行, ...

  6. install nginx error

    the error info : the HTTP gzip module requires the zlib library.You can either disable the module by ...

  7. wget安装更新

    #查看当前wget版本信息 wget -V #下载 wget https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz #解压 tar xvf wget-1.19.t ...

  8. 内存管理总结-autoreleasePool

    转自其他 序言 无论是在MRC时期还是ARC时期,做过开发的程序员都接触过autoreleasepool.尽管接触过但本人对它还不是很了解.本文只是将自己的理解说出来.在内存管理的文章中提到了OC的内 ...

  9. 认识MySQL Replication

    MySQL Replication 是 MySQL 非常有特色的一个功能,他能够将一个 MySQL Server 的 Instance 中的数据完整的复制到另外一个 MySQL Server 的 In ...

  10. java将字段映射成另一个字段,关于 接口传参 字段不对应转换

    在接口开发中我们经常会遇到一个问题,打个比方,我们的实体类A中有两个字段user和pwd但是接口中需要username和password这怎么办呢,我想到了两种方法:1.新创建一个实体类B或者new一 ...