原题链接在这里:https://leetcode.com/problems/permutation-in-string/description/

题目:

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:

  1. Input:s1 = "ab" s2 = "eidbaooo"
  2. Output:True
  3. Explanation: s2 contains one permutation of s1 ("ba").

Example 2:

  1. Input:s1= "ab" s2 = "eidboaoo"
  2. Output: False

Note:

  1. The input strings only contain lower case letters.
  2. The length of both given strings is in range [1, 10,000].

题解:

如何知道s1是s2某一段的permutation. 只要确定这一段的char count相等就行.

利用sliding window 长度为s1.length累计char count.

Note: check sum == 0 before moving walker. "ab", "bao", runner = 2, walker = 0, check sum == 0.

Time Complexity: O(n). n = s1.length()+s2.length().

Space: O(1).

AC Java:

  1. class Solution {
  2. public boolean checkInclusion(String s1, String s2) {
  3. if(s1 == null || s2 == null || s1.length() > s2.length()){
  4. return false;
  5. }
  6.  
  7. int [] map = new int[256];
  8. for(int i = 0; i<s1.length(); i++){
  9. map[s1.charAt(i)]++;
  10. }
  11.  
  12. int walker = 0;
  13. int runner = 0;
  14. int sum = s1.length();
  15. while(runner < s2.length()){
  16. if(map[s2.charAt(runner++)]-- > 0){
  17. sum--;
  18. }
  19.  
  20. if(sum == 0){
  21. return true;
  22. }
  23.  
  24. if(runner-walker==s1.length() && map[s2.charAt(walker++)]++ >= 0){
  25. sum++;
  26. }
  27. }
  28.  
  29. return false;
  30. }
  31. }

类似Find All Anagrams in a String.

LeetCode Permutation in String的更多相关文章

  1. [LeetCode] Permutation in String 字符串中的全排列

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

  2. 567. Permutation in String

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

  3. [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 ...

  4. 【LeetCode】567. Permutation in String 解题报告(Python)

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

  5. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. [Swift]LeetCode567. 字符串的排列 | 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] Custom Sort String 自定义排序的字符串

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...

  8. #Leetcode# 942. DI String Match

    https://leetcode.com/problems/di-string-match/ Given a string S that only contains "I" (in ...

  9. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

随机推荐

  1. 树 & 二叉树

    2018-01-04 19:13:46 一.树 在计算机科学中,树(英语:tree)是一种数据结构,用来模拟具有树状结构性质的数据集合.它是由n(n>0)个有限节点组成一个具有层次关系的集合.把 ...

  2. asp.net mvc Route路由映射.html后缀 404错误

    [HttpGet] [Route("item/{id:long:min(1)}.html")] 首先RouteConfig配置文件RegisterRoutes方法添加以下代码: r ...

  3. Eclipse创建Maven聚合项目

    整体架构图 1.新建父工程 新建maven父项目(用来管理jar包版本),使子系统使用同一个版本的jar包. File->New->Other->Maven Project,打包方式 ...

  4. Eclipse-环境搭建(缅怀篇)

    JDK 下载jdk安装并配置环境变量运行java -version查看是否安装配置成功 Eclipse 下载eclipse,直接解压到目录 eclipse配置jre 设置complie编译等级 Ecl ...

  5. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  6. 互换CapsLock和Ctrl键

    如果你没有HHKB键盘,完全可以利用系统自身的功能交换CapsLock和Ctrl键. macOS系统 在系统偏好设置里,点击“键盘”,在出现的画面点击右下角的“修饰键...”按钮,在这里可以配置这两个 ...

  7. Visual Studio 2017再现C语言经典例题(一)

    1.编写一个程序,输入a.b.c这3个值,输出其中最大者. 2.将“China”译成密码.密码规律:用原来的字母后面第4个字母代替原来的字母.例如,字母A后面第4个字母是E,用E代替A,因此,Chin ...

  8. bzoj2501

    题解: 显然,每当进入一个小的边界,那么我们的ans+1,出去一个大的边界,ans-1 然后,我们将每一个边界排序,时间小的在前,大的在后 每一次进来一个,如果是左边的边界,+1,右边的-1 然后输出 ...

  9. droppable放置组件

    Droppable 放置组件 所谓放置,就将一个事物入一个事物内触发各种效果,这个组件不依赖于其他组件.1.加载方式 //class 调用 <div id="dd" clas ...

  10. L159

    Waves are the children of the struggle between ocean and atmosphere, the ongoing signatures of infin ...