567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation
[抄题]:
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的更多相关文章
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- 在SQLSERVER中如何检测一个字符串中是否包含另一个字符串
--当charindex返回值大于0时则包含 为0不包含 select CHARINDEX('456','123456') SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字 ...
- 在sql server中如何检测一个字符串中是否包含另一个字符串
select CHARINDEX('456','123456') SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字符串中的方法: 一.CHARINDEX函数介绍 1.函数功 ...
- php 判断字符串中是否包含另一个字符串 strpos
strpos (PHP 4, PHP 5, PHP 7) strpos — 查找字符串首次出现的位置 说明 strpos ( string $haystack , $needle [, int $o ...
- 判断一个字符串中是否包含另一个字符串(KMP、BF)
判断一个字符串是否是另一个字符串的子串,也就是strstr()函数的实现,简单的实现方法是BF算法. 1.BF算法 int BF(char *s, char *p){ ; ; int j; while ...
- oracle判断一个字符串中是否包含另外一个字符串
select * from a where instr(a,b)>0; 用于实现B字段是A字段中的某一部分的时候,要论顺序或者要相邻的字符. 如果想要不论顺序或者不相邻的字符时,定义函数可以实现 ...
- C# 如何判断字符串中是否包含另一个字符串?
如 字符串1(str1)为:“你好怎么解决呢!” 字符串2(str2)为:“你好” 如果str1里面包str2 则 Response.Write("成功");否则 Resp ...
- 用JAVA写查询一个字符串中是否包含另外一个字符串以及出现的次数
package JAVA; import java.awt.List;import java.util.ArrayList;/** * * @author 梁小鱼 * */public class ...
- 【转载】C#通过IndexOf方法判断某个字符串是否包含在另一个字符串中
C#开发过程中针对字符串String类型的操作是常见操作,有时候需要判断某个字符串是否包含在另一个字符串,此时可以使用IndexOf方法以及Contain方法来实现此功能,Contain方法返回Tru ...
随机推荐
- Inotify机制的简单应用
编程之路刚刚开始,错误难免,希望大家能够指出. 一.Inotify机制 1.简单介绍inotify:Inotify可用于检测单个文件,也可以检测整个目录.当检测的对象是一个目录的时候,目录本身和目录里 ...
- Property ‘password’ threw Exception
问题描述: Maven项目在tomcat启动的时候总是报Propety 'password' threw exception异常时,说明password不对,但核对之后没有问题 解决方案: 核对pas ...
- laravel的filter()方法的使用 (方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容)
filter 方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容: $collection = collect([1, 2, 3, 4]); $filtered = $collec ...
- MNIST数据可视化
一.数据准备 二.数据说明 可以看出图片数据在偏移量为第16字节开始存,每28X28字节存放一张手写字图片.而label是从偏移量为第8字节开始存,每个字节存放一个label. 三.matlab201 ...
- verilog 代码分析与仿真
verilog 代码分析与仿真 注意:使用vivado 自带的仿真工具, reg和wire等信号需要赋予初始值 边沿检测 module signal_test( input wire cmos_pcl ...
- XShell停止滚屏,禁止滚动
Ctrl+S:锁定当前屏幕 Ctrl+Q:解锁当前屏幕 Ctrl+Alt+] 进入命令输入状态
- 用EXCEL做快速傅立葉轉換_FFT in Excel
转载来自:http://yufan-fansbook.blogspot.tw/2013/09/excel-fft-fast-fourier-transform02.html [Excel]-用EXCE ...
- 使用docker compose编排容器
一.安装docker compose 二进制包安装 1.安装 Docker Compose 从 官方 GitHub Release 处直接下载编译好的二进制文件即可 # curl -L https:/ ...
- 8、Curator的监听机制
原生的zookeeper的监听API所实现的方法存在一些缺点,对于开发者来说后续的开发会考虑的细节比较多. Curator所实现的方法希望摒弃原声API 的不足,是开发看起来更加的简单,一些重连等操作 ...
- 刘志梅201771010115.《面向对象程序设计(java)》第三周学习总结
实验三 Java基本程序设计(2) 实验时间 2018-9-13 1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3) ...