LeetCode Permutation in String
原题链接在这里: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:
- The input strings only contain lower case letters.
- 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的更多相关文章
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- 567. Permutation in String
Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...
- [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 ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [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 ...
- [LeetCode] Custom Sort String 自定义排序的字符串
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- #Leetcode# 942. DI String Match
https://leetcode.com/problems/di-string-match/ Given a string S that only contains "I" (in ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
随机推荐
- 【Jmeter】压测mysql数据库中间件mycat
背景 因为博主所负责测试的项目需要数据库有较大的吞吐量,在最近进行了升级,更新了一个数据库中间件 - - mycat.查询了一些资料,了解到这是阿里的一个开源项目,基于mysql,是针对磁盘的读与写, ...
- Java中的数据结构及排序算法
(明天补充) 主要是3种接口:List Set Map List:ArrayList,LinkedList:顺序表ArrayList,链表LinkedList,堆栈和队列可以使用LinkedList模 ...
- PHP libevent函数基本介绍
3.2 主要函数介绍 按照使用libevnet库顺序,看一下相关函数做什么操作. 3.2.1 event_init 调用event_base_new,初始化struct event_base对象 ...
- TestNG入门--安装和基本介绍
TestNG介绍 TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit, 功能都差不多, 只是功能更加强大,使用也更方便 Java中已经有一个JUnit的测试框架了. Tes ...
- 微信小程序------媒体组件(视频,音乐,图片)
今天主要是简单的讲一下小程序当中的媒体组件,媒体组件包括:视频,音乐,图片等. 先来看看效果图: 1:图片Image <!-- scaleToFill:不保持纵横比缩放图片,使图片的宽高完全拉伸 ...
- 剑指offer算法总结
剑指offer算法学习总结 节选剑指offer比较经典和巧妙的一些题目,以便复习使用.一部分题目给出了完整代码,一部分题目比较简单直接给出思路.但是不保证我说的思路都是正确的,个人对算法也不是特别在行 ...
- linux下鼠标穿透和取消穿透--linux小白,大神无视
最近在用qt写一个跨平台的软件,因为设置了无边框,并且我自己给程序窗口加了阴影,阴影范围又比较大 所以必须给阴影区域加上鼠标穿透才能有更好的体验. 上网查了一下,在windows下使用SetWindo ...
- js中的真值和假值
大多数编程语言中,布尔值true和false仅仅表示true/false.JavaScript中,如'Hello‘这样的字符串值,也可以看做true. 以下是不同数据类型在JavaScript中是如何 ...
- TCP/IP网路协议复习
1.OSI (Open System Interconnect Protocol) 开放互联协议,这是一个七层的计算机网络协议,包括:物理层.数据链路层.网络层.传输层.回话层.表示层.应用层. ...
- ZOJ 2283 Challenge of Wisdom 数论,Dilworth Theorem,求最长反链 难度:2
Challenge of Wisdom Time Limit: 2 Seconds Memory Limit: 32768 KB Background "Then, I want ...