原题链接在这里: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:

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

Example 2:

Input:s1= "ab" s2 = "eidboaoo"
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:

 class Solution {
public boolean checkInclusion(String s1, String s2) {
if(s1 == null || s2 == null || s1.length() > s2.length()){
return false;
} int [] map = new int[256];
for(int i = 0; i<s1.length(); i++){
map[s1.charAt(i)]++;
} int walker = 0;
int runner = 0;
int sum = s1.length();
while(runner < s2.length()){
if(map[s2.charAt(runner++)]-- > 0){
sum--;
} if(sum == 0){
return true;
} if(runner-walker==s1.length() && map[s2.charAt(walker++)]++ >= 0){
sum++;
}
} return false;
}
}

类似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. 在Web API 2 中实现带JSON的Patch请求

    译文:http://www.cnblogs.com/kexxxfeng/p/the-patch-verb-in-web-api-2-with-json.html 原文:https://carly.io ...

  2. Java 练习题摘录

    2018-01-21 23:23:08 1.finally与return同时出现的情况 finally中有return语句则会使try...catch中的return语句失效 public stati ...

  3. Linux命令详解-cal

    cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历."阳历"又名"太阳历",系以地球绕行太阳一周为一年,为西方各国所通用,故 ...

  4. Linux系统 SSHD服务安全优化方案

      # 1. 修改默认端口 #Port 22 # 2. 修改监听协议,只监听某个或某些网络协议 #AddressFamily any AddressFamily inet # 3. 修改ssh只监听内 ...

  5. 【Windows】netsh动态配置端口转发

    文章转载自傲风 使用多个虚拟机,将开发环境和工作沟通环境分开(即时通,办公系统都只能在windows下使用-),将开发环境的服务提供给外部访问时,需要在主机上通过代理配置数据转发. VirtualBo ...

  6. day5-subprocess模块

    一.概述 实际应用中,有些时候我们不得不跟操作系统进行指令级别的交互,如Linux中的shell.Python中早期通过os模块和commands模块来实现操作系统级别的交互,但从2.4版本开始,官方 ...

  7. [WinForm]FastColoredTextBox控件(附源码)

    Fast Colored TextBox is text editor component for .NET. Allows you to create custom text editor with ...

  8. java中的一些执行顺序,代码块,静态,构造,成员。。。。(转的)

    Java初始化顺序(转来的) 1在new B一个实例时首先要进行类的装载.(类只有在使用New调用创建的时候才会被java类装载器装入) 2,在装载类时,先装载父类A,再装载子类B3,装载父类A后,完 ...

  9. 剑指offer--45.二叉树的深度

    时间限制:1秒 空间限制:32768K 热度指数:139716 题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. -- ...

  10. Faster R-CNN改进篇(二): RFCN ● RON

    @改进1:RFCN 论文:R-FCN: Object Detection via Region-based Fully Convolutional Networks    [点击下载] MXNet代码 ...