[leetcode-567-Permutation in String]
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]的更多相关文章
- [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
Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...
- 【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 ...
- 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation
[抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...
- 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:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- [刷题]算法竞赛入门经典(第2版) 5-6/UVa1595 - Symmetry
题意:平面上给若干点,问它们是不是关于某垂直于x轴的直线对称. 代码:(Wrong Answer, –ms) //UVa1595 - Symmetry #include<iostream> ...
- Socket中的异常和参数设置
1.常见异常 1.java.net.SocketTimeoutException . 这个异 常比较常见,socket 超时.一般有 2 个地方会抛出这个,一个是 connect 的 时 候 , 这 ...
- GreenDao
前言 我相信,在平时的开发过程中,大家一定会或多或少地接触到 SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写 SQL 语句与解析查询结果等.所以,适用于 Android 的ORM ...
- Listener与Filter
一.监听器Listener javaEE的13们规范中 包括servlet技术和jsp技术 servlet规范中包括三门技术:(servlet的三大组件) servelt技术 Listener技术 ...
- 多工程:基于Maven的SSM(Spring,SpringMvc,Mybatis)整合的web工程(中)
上篇用了单工程创建了SSM整合的web工程(http://www.cnblogs.com/yuanjava/p/6748956.html),这次我们把上篇的单工程改造成为多模块工程 一:创建对应的多工 ...
- OpenCV探索之路(十三):详解掩膜mask
在OpenCV中我们经常会遇到一个名字:Mask(掩膜).很多函数都使用到它,那么这个Mask到底什么呢? 一开始我接触到Mask这个东西时,我还真是一头雾水啊,也对无法理解Mask到底有什么用.经过 ...
- 无锁模式的Vector
这两天学习无锁的并发模式,发现相比于传统的 同步加锁相比,有两点好处1.无锁 模式 相比于 传统的 同步加锁 提高了性能 2.无锁模式 是天然的死锁免疫 下来介绍无锁的Vector--- LockF ...
- Mac电脑如何搭建php环境,并且开发php.
这篇文章主要介绍了Mac下搭建php开发环境教程,Mac OS X 内置了Apache 和 PHP,这样使用起来非常方便.本文以Mac OS X 10.12.4为例,需要的朋友可以参考下! Mac O ...
- Reinforcement Learning in R
Reinforcement learning has gained considerable traction as it mines real experiences with the help o ...
- Interactive pivot tables with R(转)
I love interactive pivot tables. That is the number one reason why I keep using spreadsheet software ...