网址: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. centos7安装gcc7.2.0

    1.有时候安装某些工具时,会提示gcc版本过低,需要安装更高版本. 进入/usr/src目录 cd usr/src 2.下载gcc7.2.0 wget 'http://mirrors-usa.go-p ...

  2. 在dotnetcore的MVC项目中,创建支持 vue.js 的最小工程模板

    网上Vue模板不是最新的,我自己做了一个最新的支持 Vue.js 的最小工程模板,方便大家从 Hello world. 入门, 在 VS2017 里学习,并扩展出自己的项目. 下面是创建步骤: 1.在 ...

  3. CCF CSP 201809-2 买菜

    题目链接:http://118.190.20.162/view.page?gpid=T78 问题描述 小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁边的一个广 ...

  4. centos6.5 MySQL数据库的安装

    <div id="home"><div id="header"> <div id="blogTitle"> ...

  5. 使用GoldenGate EVENTACTIONS执行数据的实时触发和定制化

    Oracle GoldenGate不仅可以在线实时同步数据(包括增量和存量),也内置有一套事件触发流程,允许用户根据某张表某条记录的某个特殊字段值,触发相应的自定义执行流程,比如接收到某个银行账号的大 ...

  6. 手机APP应用外网访问本地WEB应用

    手机APP应用外网访问本地WEB应用 本地安装了WEB服务端,手机APP应用只能在局域网内访问本地WEB,怎样使手机APP应用从公网也能访问本地WEB? 本文将介绍具体的实现步骤. 1. 准备工作 1 ...

  7. 【Alpha】Scrum Meeting 8

    目录 前言 任务分配 燃尽图 会议照片 签入记录 困难 前言 第8次会议在4月12日21:00进行微信会议. 交流确认了任务进度,对下一阶段任务进行分配.时长15min. 任务分配 姓名 当前阶段任务 ...

  8. JS(JavaScript)的初了解8(更新中···)

    1.函数都有返回值…… 而方法的本质也是函数,所以也有返回值. Document.getElementById() 返回的是获取的标签 getElementsByClassName()和getElem ...

  9. C# Post 参数中的特殊符号处理

    https://cloud.tencent.com/developer/article/1344673 http://blog.csdn.net/henulwj/article/details/791 ...

  10. 必会SQL练习题

    ()表名:购物信息 购物人 商品名称 数量 A 甲 B 乙 C 丙 A 丁 B 丙 …… 给出所有购入商品为两种或两种以上的购物人记录 答:); ()表名:成绩表 姓名 课程 分数 张三 语文 张三 ...