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

参考:https://leetcode.com/problems/permutation-in-string/discuss/102588/Java-Solution-Sliding-Window

把s1的各个字母的出现次数记录下来,对s2利用滑动窗口法,逐步移动窗口,判断当前窗口内字符串是否是s1的全排列之一。
容易想到,窗口内的字符对应的出现次数要-1。所以,当字符从窗口左端脱离窗口时,要+1;当字符从窗口右端进入窗口时,要-1。

class Solution
{
public:
bool allZero(vector<int> count) // 检查count是否全零
{
for(int i : count)
{
if( i != )
return false;
}
return true;
} bool checkInclusion(string s1, string s2)
{
int len1 = s1.size();
int len2 = s2.size();
// if s1 is longer than s2, the answer is definitely false
if(len1 > len2)
return false; // 用于保存26个字母的使用情况
vector<int> count(, );
for(int i=; i<len1; i++)
{
// 将s1的各个字母使用次数加 1
count[s1[i] - 'a']++;
// 此语句实际上为滑动窗口的初始化
count[s2[i] - 'a']--;
}
// 判断第一个滑动窗口
if(allZero(count))
return true; // 移动滑动窗口
for(int i=len1; i<len2; i++)
{
//
count[s2[i] - 'a']--;
count[s2[i-len1] - 'a']++;
// 检查是否全零。全零代表当前滑动窗口为s1的全排列之一
if(allZero(count))
return true;
} return false;
}
};

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. [CareerCup] 1.3 Permutation String 字符串的排列

    1.3 Given two strings, write a method to decide if one is a permutation of the other. 这道题给定我们两个字符串,让 ...

  3. 567. Permutation in String

    Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...

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

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

  5. C#的StringBuilder 以及string字符串拼接的效率对照

    今天公司一个做Unity3d的人在说字符串拼接的一个效率问题,他觉得string拼接会产生新的一个内存空间,假设不及时回收会产生大量的碎片,特别是在Unity3d这样一个Updata环境下,由于每一帧 ...

  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. 【LeetCode】567. Permutation in String 解题报告(Python)

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

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

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

  9. String字符串性能优化的探究

    一.背景 String 对象是我们使用最频繁的一个对象类型,但它的性能问题却是最容易被忽略的.String 对象作为 Java 语言中重要的数据类型,是内存中占用空间最大的一个对象,高效地使用字符串, ...

随机推荐

  1. ASP.NET页面之间传值的方式之Cookie(个人整理)

    Cookie Cookie 提供了一种在 Web 应用程序中存储用户特定信息的方法.例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息.当该用户再次访问您的网站时,应用程序 ...

  2. pycharm2018安装教程 pycharm2018永久激活教程

    安装教程 下载pycharm 2018.3.2安装文件,可以直接点击下载网盘下载 激活码地址:http://demo.liuy88.cn/jp0876.html 下载完成后,双击exe即可开始安装 点 ...

  3. laravel ServiceProvider 加载顺序

    主要看一下代码: public function registerConfiguredProviders(){//读取app的配置,然后,分成两部分Illuminate开始的,和其他的 $provid ...

  4. webdriver保存验证码截图

    element = wait.until ( EC.visibility_of_element_located((By.CSS_SELECTOR,'.quc-main .quc-field-captc ...

  5. 7.JAVA基础复习——JAVA中的设计模式单例模式

    设计模式:是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 总体来说设计模式分为23种三大类: 创建型模式,共 ...

  6. String<-->int

    String s = "123); int a = Integer.parseInt(s); String b = String.valueOf(a); Integer i = 100; 自 ...

  7. pyinstaller

    下载pyinstaller pip install pyinstaller 打包文件 pyinstaller -F run.py # 打包成单个文件

  8. Parallels Desktop 14.1.3中文版win系统安装教程

    parallels desktop 14 中文版(pd虚拟机)是mac上最强大也是最好用的虚拟机软件,本站第一时间为大家带来这款parallels desktop 14 破解版,最新版本的parall ...

  9. oracle 表空间管理相关(原创)

    通过以下几步基本可以查看表空间情况以及处理表空间不足问题. ASM相关 查看asm空间 select group_number,name,total_mb,free_mb from v$asm_dis ...

  10. [Python数据挖掘]第6章、电力窃漏电用户自动识别

    一.背景与挖掘目标 相关背景自查 二.分析方法与过程 1.EDA(探索性数据分析) 1.分布分析 2.周期性分析 2.数据预处理 1.数据清洗 过滤非居民用电数据,过滤节假日用电数据(节假日用电量明显 ...