567. Permutation in String
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的更多相关文章
- 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation
[抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...
- [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【滑动窗口】
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- 【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 ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- [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 ...
- 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 ...
- LeetCode Permutation in String
原题链接在这里:https://leetcode.com/problems/permutation-in-string/description/ 题目: Given two strings s1 an ...
随机推荐
- Service官方教程(1)Started与Bound的区别、要实现的函数、声明service
Services 简介和分类 A Service is an application component that can perform long-running operations in the ...
- 2016/10/29 Action类中execute方法的使用
第一步:先配置web.xml文件 <filter> <filter-name>struts2</filter-name> <filter-class>o ...
- jmeter(十七)逻辑控制器
JMeter中的Logic Controller用于为Test Plan中的节点添加逻辑控制器. JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行过程中节点 ...
- 224 Basic Calculator 基本计算器
实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式可以包含左括号 ( ,右括号),加号+ ,减号 -,非负整数和空格 . 假定所给的表达式语句总是正确有效的. 例如: "1 + ...
- Android插件开发
插件开发的概念: 对于一个功能特别多,代码量特别大的App比如支付宝.360手机助手来说,如果把所有的功能和代码都写在一个App中,就会造成App体积过于庞大,用户下载体验差,不方便测试,业务.模块耦 ...
- 用RecyclerView做一个小清新的Gallery效果
一.简介 RecyclerView现在已经是越来越强大,且不说已经被大家用到滚瓜烂熟的代替ListView的基础功能,现在RecyclerView还可以取代ViewPager实现Banner效果,当然 ...
- leetcode:single-number-ii(Java位运算)
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- 遮罩 HUD 指示器 蒙板 弹窗
遮罩 HUD 指示器 蒙板 弹窗 UIAlertView的使用<代理方法处理按钮点击> UIAlertView *alertView = [[UIAlertView alloc] init ...
- 契约式设计(DbC)感想(二)
契约式设计6大原则的理解 在<Design by Contract原则与实践>中,作者定义了契约式设计的6大原则: 区分命令和查询: 将基本查询和派生查询区分开: 针对每个派生查询,设定一 ...
- Node.js——Buffer
介绍 JavaScript没有读取和操作二进制数据流的机制,但是 node.js 引入了Buffer 类型,可以操作TCP流或者文件流 使用Buffer可以用来对临时数据(二进制数据)进行存储,当我们 ...