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 ...
随机推荐
- js数组与字符串的相互转换
一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b,c; a = new Array(a,b,c,d,e); b = a.join('-'); //a-b-c-d ...
- 使用idea引入注解@SpringBootApplication报错Cannot resolve symbol 'SpringBootApplication'
我在使用idea时,在类上使用注解@SpringBootApplication,但是一直报错. Cannot resolve symbol 'SpringBootApplication' 网络上有很多 ...
- nyoj299——如何优雅的写矩阵快速幂
Matrix Power Series 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a n × n matrix A and a positive i ...
- IOS-CALayer(图层)
BWLayer.m // // BWLayer.m // IOS_0222_CALayer // // Created by ma c on 16/2/23. // Copyright © 2016年 ...
- IOS-每个程序员的编程之路上都应该看这11本书
国外知名网站stackoverflow上有一个问题调查: 哪本书是对程序员最有影响.每个程序员都该阅读的书?,这个调查已历时两年,目前为止吸引了153,432人访问,读者共推荐出了478本书(还在增加 ...
- day28 CRM万能权限组件开发 && 主机管理-堡垒机
1,CRM项目实战-万能权限组件开发参考博客:http://www.cnblogs.com/alex3714/articles/6661911.html 参考代码:https://github.com ...
- Falsy Bouncer
真假美猴王! 删除数组中的所有假值. 在JavaScript中,假值有false.null.0."".undefined 和 NaN. 这是一些对你有帮助的资源: Boolean ...
- 备注下Windows可能会用到的运行命令
因为有几个命令不常用忘记了,所以备注下Windows可能会用到的运行命令: 1.cleanmgr:打开磁盘清理工具2.compmgmt.msc:计算机管理3.charmap:启动字符映射表4.calc ...
- RESTful设计方法
REST REST,即Representational State Transfer的缩写.维基百科称其为“具象状态传输”,国内大部分人理解为“表现层状态转化”. RESTful是一种开发理念.维基百 ...
- java基础 题和知识点总结, 关于String s是否默认初始化为null......,new一个对象和类静态域,是不是在内存中不是一个地方
一道笔试题 22. 下面代码的运行结果为:() import java.io.*; import java.util.*; public class foo{ public static void m ...