Problem
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].
Solution
</>复制代码
class Solution {
public boolean checkInclusion(String s1, String s2) {
int l1 = s1.length(), l2 = s2.length();
if (l2 < l1) return false;
Map map = new HashMap<>();
for (int i = 0; i < l1; i++) {
map.put(s1.charAt(i), map.getOrDefault(s1.charAt(i), 0)+1);
map.put(s2.charAt(i), map.getOrDefault(s2.charAt(i), 0)-1);
}
if (checkMap(map)) return true;
for (int i = l1; i < l2; i++) {
map.put(s2.charAt(i), map.getOrDefault(s2.charAt(i), 0)-1);
map.put(s2.charAt(i-l1), map.get(s2.charAt(i-l1))+1);
if (checkMap(map)) return true;
}
return false;
}
private boolean checkMap(Map map) {
for (Map.Entry entry: map.entrySet()) {
if (entry.getValue() != 0) return false;
}
return true;
}
}
using int array as map
</>复制代码
class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() > s2.length()) return false;
int[] count = new int[26];
for (int i = 0; i < s1.length(); i++) {
count[s1.charAt(i)-"a"]++;
count[s2.charAt(i)-"a"]--;
}
if (allZero(count)) return true;
for (int i = s1.length(); i < s2.length(); i++) {
count[s2.charAt(i)-"a"]--;
count[s2.charAt(i-s1.length())-"a"]++;
if (allZero(count)) return true;
}
return false;
}
private boolean allZero(int[] arr) {
for (int i: arr) {
if (i != 0) return false;
}
return true;
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71904.html
摘要:找规律复杂度时间空间思路由于我们只要得到第个全排列,而不是所有全排列,我们不一定要将所有可能都搜索一遍。根据全排列顺序的性质,我们可以总结出一个规律假设全排列有个数组成,则第个全排列的第一位是。然后将得到,这个就是下一轮的。 Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutati...
摘要:题目要求假设按照题中给的排列组合的顺序,假设有个数字,返回第个排列组合的结果。最后在个位上,选择中的第一个。这时知道以第位为开头的结果值有此时第个结果集在该位上的选择为。依次往后类推,直至到最后一位。 题目要求 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling...
摘要:最笨的方法就是用的解法,找出所有的,然后再用中判断回文的方法来判断结果中是否有回文。而中心对称点如果是字符,该字符会是奇数次,如果在两个字符之间,则所有字符都是出现偶数次。 Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. F...
摘要:因为增加高位会带来更大的增益。所以对于一个长为的序列,我们增加第位的前提是,前位已经达到了最大排列方法。因为是找下一个数,所以我们要找一个比小却尽可能大的数,所以找到。把换到的位置后,后三位仍然是个降序的排列。 Next Permutation Implement next permutation, which rearranges numbers into the lexicogr...
Problem Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. Example Given s = aabb, return [abba,baa...
阅读 1238·2021-09-22 15:32
阅读 1803·2019-08-30 15:53
阅读 3326·2019-08-30 15:53
阅读 1480·2019-08-30 15:43
阅读 529·2019-08-28 18:28
阅读 2666·2019-08-26 18:18
阅读 758·2019-08-26 13:58
阅读 2610·2019-08-26 12:10