网址: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. echarts 图的点击事件(含:点击重复触发的问题及其解决方法)

    今天用echarts的时候发现一个问题 鼠标指向不同地市触发一个事件展示该地区趋势图  但是但是后台中不管我第几次鼠标指向都会触发两次指向事件 现在贴出解决办法: 问题完美解决.但是为什么会调用两次, ...

  2. ARM LCD屏调试3--屏的应用编程

    2011-06-25 19:20:47 驱动自己写完了,应用函数自己就不写了,找了一点代码参考,移植并修改了一下,配合之前的定义的接口文档,我贴出部分代码.目录: 一,开发环境... 1 二,底层函数 ...

  3. redis 在 php 中的应用(List篇)

    本文为我阅读了 redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法) 目录: List(列表) LPUSH LPUSHX RPUSH R ...

  4. c语言的一些易错知识积累

    1. #ifdef  和#if defined 的区别: 后者可以组成复杂的预编译条件,而如果判断的是单个宏定义的时候,两种用法的效果都是一样的. 2.#if 0 { code }#endif    ...

  5. 利用 postMessage 进行数据传递 (iframe 及web worker)及问题

    一 postMessage应用于主页面和iframe之间进行数据的传递 1  子iframe页面向主页面进行数据传递: // 多个子iframe需要将自己的计数统计到主页面进行数据上报 window. ...

  6. ACTIVEMQ 实例化到MSSQL

    实例化文章很多,不重复,自行查询 直接上XML <!-- Licensed to the Apache Software Foundation (ASF) under one or more c ...

  7. webviewplugin

    https://blog.csdn.net/qq_39197547/article/details/85007418 https://www.cnblogs.com/pjl43/p/9866753.h ...

  8. VB代码收集

    1.随机获取5位验证码? 需求: 创建一个Label1:名称为随机验证码生成 创建一个Label2:名称为为空,属性BorderStyle=1 创建一个CommandButton:名称为获取随机码 代 ...

  9. C# 开发AliYun(阿里云) 小蜜调用接口代码

    using System; using System.Collections.Generic; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Excepti ...

  10. day 07

    # 数据类型的相互转化# 字符编码# 文件操作 # 1.哪些类型可以转化为数字# res = int('10')# print(res)# res = int('-3')# print(res)# r ...