原题链接在这里: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. CSP(Content Security Policy) 入门教程

    参考: http://www.ruanyifeng.com/blog/2016/09/csp.html https://developer.mozilla.org/en-US/docs/Web/HTT ...

  2. [mybatis]Record与Example的用法

    一.Record 一个Record是一个Dao对象(继承Mapper接口),tkmybatis会将record自动映射成sql语句,record中所有非null的属性都作为sql语句,如: 映射的sq ...

  3. 一个很有用的树形控件----zTree

    演示地址 http://www.treejs.cn/v3/demo.php#_101

  4. flask学习(六):URL传参

    1. 参数的作用:可以在相同的URL,但是指定不同的参数,来加载不同的数据 例如:简书上每一篇文章前面的URL相同,只是后面的参数不同 2. 在flask中如何使用参数: 注意: 1) 参数需要放在两 ...

  5. torch Tensor学习:切片操作

    torch Tensor学习:切片操作 torch Tensor Slice 一直使用的是matlab处理矩阵,想从matlab转到lua+torch上,然而在matrix处理上遇到了好多类型不匹配问 ...

  6. 卸载Linux自带的JDK

    Redhat Enterprise Linux中自带了jdk的旧版本,往往需要卸载,卸载步骤如下: 在终端输入:yum remove java 终端显示:Is this ok[y/N]: 输入y,按回 ...

  7. Hrbust 1535 相爱

    Description 静竹在斐波那契的帮助下,渐渐的和数学相爱了.和数学在一起最有意思的就是它能够出一些特别有意思并且巧妙的题目让静竹来思考.这次也不例外,给静竹两个数a,b,又给出了加,减,乘,除 ...

  8. MySQL Batched Key Access

    Batched Key Access是MySQL 5.6 版本中的新特性,是一种用户提高表join性能的算法.[Batched Key Access]       对于多表join语句,当MySQL使 ...

  9. procrdure存储过程

    /* 存储过程 在一些语言中,有一个概念叫”过程“ procedure,和”函数“ function 过程:封装了若干条语句,调用时,这些封装体执行 函数:是一个有返回值的“过程” 过程:没有返回值的 ...

  10. 【dlbook】实践方法论

    [性能度量] 使用什么误差度量? 目标性能大致为多少? [默认的基准模型] 首先尝试分段线性单元,ReLU以及扩展. SGD一般是合理的选择,选加入动量的版本,衰减方法不一. 批标准化在优化出现问题时 ...