网址: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。

  1. class Solution
  2. {
  3. public:
  4. bool allZero(vector<int> count) // 检查count是否全零
  5. {
  6. for(int i : count)
  7. {
  8. if( i != )
  9. return false;
  10. }
  11. return true;
  12. }
  13.  
  14. bool checkInclusion(string s1, string s2)
  15. {
  16. int len1 = s1.size();
  17. int len2 = s2.size();
  18. // if s1 is longer than s2, the answer is definitely false
  19. if(len1 > len2)
  20. return false;
  21.  
  22. // 用于保存26个字母的使用情况
  23. vector<int> count(, );
  24. for(int i=; i<len1; i++)
  25. {
  26. // 将s1的各个字母使用次数加 1
  27. count[s1[i] - 'a']++;
  28. // 此语句实际上为滑动窗口的初始化
  29. count[s2[i] - 'a']--;
  30. }
  31. // 判断第一个滑动窗口
  32. if(allZero(count))
  33. return true;
  34.  
  35. // 移动滑动窗口
  36. for(int i=len1; i<len2; i++)
  37. {
  38. //
  39. count[s2[i] - 'a']--;
  40. count[s2[i-len1] - 'a']++;
  41. // 检查是否全零。全零代表当前滑动窗口为s1的全排列之一
  42. if(allZero(count))
  43. return true;
  44. }
  45.  
  46. return false;
  47. }
  48. };

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. npm install --save 、--save-dev 、-D、-S 的区别

    备注:<=> 意为等价于: 1.npm install <=> npm i --save   <=> -S --save-dev  <=> -D npm ...

  2. Spark大型电商项目实战-及其改良(4) 单独运行程序发现的问题

    之前的运行结果比对发现,有1个函数的作用在2个job里面是相同的,但是对应的计算时间却差太远 于是把4个job分开运行.虽说使用的数据不同,但是生成数据的生成器是相同的,数据排布差距不大,数据量也是相 ...

  3. Vue 旅游网首页开发2 - 首页编写

    Vue 旅游网首页开发2 - 首页编写 项目结构 首页开发 效果图 项目开发组件化 将页面的各个部分划分成不同的组件,有助于项目的开发和维护. 项目代码初始化 项目结构修改 1.删除整个 compin ...

  4. java枚举类型详解

      枚举类型是JDK1.5的新特性.显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类.而这些类都是类库中Enum类的子类(java.lang.Enum<E>).它 ...

  5. 基础JAVA程序设计(多个类与方法的实现2)

    设计一个类代表二维空间的一个点(Point),要求:两个成员变量:x坐标和y坐标. 设计一个类代表二维空间的一个圆(Circle),要求:两个成员变量:一个是圆心,一个是半径:提供计算圆面积的方法:提 ...

  6. C#线程同步(1)- 临界区&Lock

    文章原始出处 http://xxinside.blogbus.com/logs/46441956.html 预备知识:线程的相关概念和知识,有多线程编码的初步经验. 一个机会,索性把线程同步的问题在C ...

  7. Python脚本备份

    #!/usr/bin/python3 # -*- coding:utf- -*- # 保证源程序可以输入汉字 print bool([]) # 任何为零的数字或空集(空列表.空元组和空字典等)均为Fa ...

  8. docker更改默认仓库地址

    zq@ubuntu:~$ docker pull -h Flag shorthand -h has been deprecated, please use --help Usage: docker p ...

  9. Oracle 《积累章》 根据身份证号码更新当前出生日期

    ,),'yyyy-MM-dd') 函数使用 to_date()      日期转换函数 to_date(substr(t.sfzh,7,8),'yyyy-MM-dd')   将“19901212” 转 ...

  10. Wannafly挑战赛1 C MMSet2 虚树

    题目链接:https://www.nowcoder.com/acm/contest/15/C 思路:虚树,取两点间的lca,构造成一颗新的树:求(直径+1)/2即可 #pragma comment(l ...