[抄题]:
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的代码] :
[潜台词] :
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// 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)); } }