[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

子串覆盖的题其实经常用sliding window,忘了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

为了减掉s1的字符,别的串是先加后减,这里是先减后加。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

s1的字符串头必须处理完才返回true,稍微理解下

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

为了减掉s1的字符,别的串是先加后减,这里是先减后加。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*; /*class TreeNode
{
int val;
TreeNode left, right; //parameter is another item
TreeNode(int item) {
val = item;
left = right = null;
}
}*/ /*
Input:s1 = "ab" s2 = "eidbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("ba").
*/ class Solution {
public boolean checkInclusion(String s1, String s2) {
//initialization counts
int len1 = s1.length(); int len2 = s2.length();
int[] counts = new int[26]; //corner case
if (len1 > len2) return false; //go through s1
for (int i = 0; i < len1; i++) {
counts[s1.charAt(i) - 'a']++;
counts[s2.charAt(i) - 'a']--;
if(allZeros(counts)) return true;
} /*
a++ = 1,e-- = -1 b++ = 1 i-- = -1 */ //go through s1 - s2
for (int i = len1; i < len2; i++) {
counts[s2.charAt(i) - 'a']--;
counts[s2.charAt(i - len1) - 'a']++;
if(allZeros(counts)) return true;
}
/*
d-- = -1 e++ = 0, */ return false;
} public boolean allZeros(int[] counts) {
//false if
for(int i = 0; i < 26; i++)
if (counts[i] != 0) return false;
return true;
}
} class MyCode {
public static void main (String[] args) {
Solution answer = new Solution();
String s1 = "ab";
String s2 = "eidbaooo"; System.out.println("result = " + answer.checkInclusion(s1, s2));
}
}

567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation的更多相关文章

  1. php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos

    php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...

  2. 在SQLSERVER中如何检测一个字符串中是否包含另一个字符串

    --当charindex返回值大于0时则包含 为0不包含 select CHARINDEX('456','123456')   SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字 ...

  3. 在sql server中如何检测一个字符串中是否包含另一个字符串

    select CHARINDEX('456','123456')   SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字符串中的方法: 一.CHARINDEX函数介绍 1.函数功 ...

  4. php 判断字符串中是否包含另一个字符串 strpos

    strpos (PHP 4, PHP 5, PHP 7) strpos — 查找字符串首次出现的位置 说明 strpos ( string $haystack ,  $needle [, int $o ...

  5. 判断一个字符串中是否包含另一个字符串(KMP、BF)

    判断一个字符串是否是另一个字符串的子串,也就是strstr()函数的实现,简单的实现方法是BF算法. 1.BF算法 int BF(char *s, char *p){ ; ; int j; while ...

  6. oracle判断一个字符串中是否包含另外一个字符串

    select * from a where instr(a,b)>0; 用于实现B字段是A字段中的某一部分的时候,要论顺序或者要相邻的字符. 如果想要不论顺序或者不相邻的字符时,定义函数可以实现 ...

  7. C# 如何判断字符串中是否包含另一个字符串?

    如  字符串1(str1)为:“你好怎么解决呢!”    字符串2(str2)为:“你好” 如果str1里面包str2 则 Response.Write("成功");否则 Resp ...

  8. 用JAVA写查询一个字符串中是否包含另外一个字符串以及出现的次数

    package JAVA; import java.awt.List;import java.util.ArrayList;/** *  * @author 梁小鱼 * */public class ...

  9. 【转载】C#通过IndexOf方法判断某个字符串是否包含在另一个字符串中

    C#开发过程中针对字符串String类型的操作是常见操作,有时候需要判断某个字符串是否包含在另一个字符串,此时可以使用IndexOf方法以及Contain方法来实现此功能,Contain方法返回Tru ...

随机推荐

  1. uip移植telnetd并加入自己定义命令

    版权声明: https://blog.csdn.net/cp1300/article/details/30541507 刚刚移植了一下uip的telnetd,还是比較简单方便的. 首先加入文件,注意u ...

  2. Game Physics Cookbook (Gabor Szauer 著)

    Chapter1: Vectors Chapter2: Matrices Chapter3: Matrix Transformations Chapter4: 2D Primitive Shapes ...

  3. sublime text 3 vue 语法高亮

    1.下载文件 链接 https://github.com/vuejs/vue-syntax-highlight 2.sublime菜单栏->Preferences->Browse Pack ...

  4. WampServer的下载方法

    http://www.wampserver.com/ 无法访问 报网络连接错误 2019.01.13 最近要用到Windows+apache+mysql+php,为了追求更快的实现速度和更高的稳定性, ...

  5. docker内存监控与压测

    一直运行的docker容器显示内存已经耗尽,并且容器内存耗尽也没出现重启情况,通过后台查看发现进程没有占用多少内存.内存的监控使用的是cadvisor,计算方式也是使用cadvisor的页面计算方式, ...

  6. pytorch下的lib库 源码阅读笔记(1)

    置顶:将pytorch clone到本地,查看initial commit,已经是麻雀虽小五脏俱全了,非常适合作为学习模板. 2017年12月7日01:24:15 2017-10-25 17:51 参 ...

  7. Schtasks命令详解(计划任务DOS批处理)

    Schtasks 安排命令和程序定期运行或在指定时间内运行.从计划表中添加和删除任务,按需要启动和停止任务,显示和更改计划任务. 创建新的计划任务. 语法 schtasks/create/tnTask ...

  8. processjs Documentation

    Documentation   Paul Nieuwelaar edited this page on 20 Sep 2017 · 4 revisions Installation & Usa ...

  9. centos 支持安装libsodium

    yum install epel-release -y yum install libsodium -y 然后没了.

  10. Master公式计算递归时间复杂度

    我们在算递归算法的时间复杂度时,Master定理为我们提供了很强大的便利! Master公式在我们的面试编程算法中除了BFPRT算法的复杂度计算不了之外,其他都可以准确计算! 这里用求数组最大值的递归 ...