Leetcode 567. Permutation in String
来源:互联网 发布:中国数据分析行业峰会 编辑:程序博客网 时间:2024/05/18 21:47
https://leetcode.com/problems/permutation-in-string/description/
https://discuss.leetcode.com/topic/87845/java-solution-sliding-window
感谢@shawngao的sliding-window的提示
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:TrueExplanation: 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].
暴力解法肯定会爆炸。
有了想统计单个char的想法,然后看见discuss中sliding-window的时候,思路一下子就豁然开朗了,通过在s2上维护一个s1长的sliding-window,然后统计这段长度中的char的数量,若与s1的数量一致,则意味着s2的这段子串是s1的一个排列。
结合javascript的语言结构,hash统计的实现很是简单
var checkInclusion = function(s1, s2) { if (s2.length < s1.length || s2.length == 0) { return false; } const hashMap = {}; const len = s1.length; for (let i = 0; i < len; ++i) { if (!hashMap[s1[i]]) { hashMap[s1[i]] = 0; } if (!hashMap[s2[i]]) { hashMap[s2[i]] = 0; } hashMap[s1[i]]++; hashMap[s2[i]]--; } if (isAllZero(hashMap)) { return true; } for (let i = len; i < s2.length; ++i) { if (!hashMap[s2[i]]) { hashMap[s2[i]] = 0; } hashMap[s2[i]]--; hashMap[s2[i-len]]++; if (isAllZero(hashMap)) { return true; } } return false;};const isAllZero = (hashMap) => { const keys = Object.keys(hashMap); for (let i = 0; i < keys.length; ++i) { if (hashMap[keys[i]]) { return false; } } return true;}
然而beat 7%,那是当然,毕竟是object,提速的话改成array试试
var checkInclusion = function(s1, s2) { if (s2.length < s1.length || s2.length === 0) { return false; } const charArr = Array(26).fill(0); const len = s1.length; const offset = 'a'.charCodeAt(0); for (let i = 0; i < len; ++i) { charArr[s1.charCodeAt(i) - offset]++; charArr[s2.charCodeAt(i) - offset]--; } if (isAllZero(charArr)) { return true; } for (let i = len; i < s2.length; ++i) { charArr[s2.charCodeAt(i) - offset]--; charArr[s2.charCodeAt(i-len) -offset]++; if (isAllZero(charArr)) { return true; } } return false;};const isAllZero = (charArr) => { for (let i = 0; i < charArr.length; ++i) { if (charArr[i]) { return false; } } return true;}
提速超明显,beat 100%,估计是Array(26).fill(0)和len,offset常量提前算的原因。
- [leetcode]567. Permutation in String
- [leetcode] 567. Permutation in String
- 【LeetCode】567. Permutation in String
- [LeetCode] 567. Permutation in String
- Leetcode 567. Permutation in String
- leetcode 567. Permutation in String 滑动窗口
- [Leetcode] 567. Permutation in String 解题报告
- LeetCode #567 Permutation in String
- 567. Permutation in String
- 567. Permutation in String
- 567. Permutation in String
- 567. Permutation in String
- Leetcode之Permutation in String 问题
- LeetCode[567]Permutation in String(Java)
- 567. Permutation in String Medium
- 567. Permutation in String 字符串置换
- Permutation in String
- Permutation in String
- 16秋计算机JAVA第三节课作业(孙家伟) 1~3题
- 在eclipse中如何设置自动联想功能 --- 一图胜千言
- 着色器显示
- 构建高性能的ASP.NET应用(16)
- 二叉树
- Leetcode 567. Permutation in String
- 统计学习方法概论
- BosonNLP API 中文语义分析(笔记)
- java-单例模式
- 拜占庭将军问题见解
- Linux常用命令
- 如何解决mysql.sock直接找不到了的问题
- Rhyme/Java 设计模式之装饰设计模式
- oj【temp】