Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Example 1:
Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start index = 0 is "ab", which is an anagram of "ab". The substring with start index = 1 is "ba", which is an anagram of "ab". The substring with start index = 2 is "ab", which is an anagram of "ab".
这道题给了我们两个字符串s和p,让我们在s中找字符串p的所有变位次的位置,所谓变位次就是字符种类个数均相同但是顺序可以不同的两个词,那么我们肯定首先就要统计字符串p中字符出现的次数,然后从s的开头开始,每次找p字符串长度个字符,来验证字符个数是否相同,如果不相同出现了直接break,如果一直都相同了,则将起始位置加入结果res中,参见代码如下:
这道题一个最重要的难点就是时间控制。另一个就是哈希表的应用。
第一次做:思想很简单,但是结果超时了:
用一个hashmap存起来p的中的信息。
遍历s,然后与p对比
复杂度:O( (s.length-p.length) * p.length) ) 类似于O(n^2)
class Solution {
public class Count{
int oldCount;
int newCount;
Count(int oldCount,int newCount){
this.oldCount = oldCount;
this.newCount = newCount;
}
}
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<Integer>();
//把p存到一个hashtable里面
HashMap<Character,Count> p_hash = new HashMap<Character, Count>();
for(int i = 0;i<p.length();i++){
if(p_hash.containsKey(p.charAt(i))){
int count = p_hash.get(p.charAt(i)).oldCount;
count ++;
p_hash.put(p.charAt(i),new Count(count,0));
}else p_hash.put(p.charAt(i),new Count(1,0));
}
for(int i= 0 ;i<=s.length()-p.length();i++){
int j ;
for(j = 0;j<p.length();j++){
if(p_hash.containsKey(s.charAt(i+j))){
int old_count = p_hash.get(s.charAt(i+j)).oldCount;
int new_count = p_hash.get(s.charAt(i+j)).newCount;
new_count ++ ;
p_hash.put(s.charAt(i+j),new Count(old_count,new_count));
if(new_count > old_count) break;
}else break;
}
if(j==p.length()) list.add(i);
for(int k = 0;k<p.length();k++){
int old_count = p_hash.get(p.charAt(k)).oldCount;
p_hash.put(p.charAt(k),new Count(old_count,0));
}
}
return list;
}
}
不需要Count这个类,只用一个count int值作为value就可以
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<Integer>();
//把p存到一个hashtable里面
HashMap<Character,Integer> p_hash = new HashMap<Character, Integer>();
for(int i = 0;i<p.length();i++){
if(p_hash.containsKey(p.charAt(i))){
int count = p_hash.get(p.charAt(i));
count ++;
p_hash.put(p.charAt(i),count);
}else p_hash.put(p.charAt(i),1);
}
for(int i= 0 ;i<=s.length()-p.length();i++){
int j ;
for(j = 0;j<p.length();j++){
if(p_hash.containsKey(s.charAt(i+j))){
int count = p_hash.get(s.charAt(i+j));
count--;
p_hash.put(s.charAt(i+j),count);
if(count<0) break;
}else break;
}
if(j==p.length()) list.add(i);
p_hash.clear();
for(int k = 0;k<p.length();k++){
if(p_hash.containsKey(p.charAt(k))){
int count = p_hash.get(p.charAt(k));
count ++;
p_hash.put(p.charAt(k),count);
}else p_hash.put(p.charAt(k),1);
}
}
return list;
}
}
然后去网上查阅资料,看看别人是怎么做的,怎么就没有超时呢?
说实话,查了一堆,感觉和我的思想没有差别啊
然后我又改了数据结构:用数组来存储,用两个数组
class Solution {
public static List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<Integer>();
//把p存到一个hashtable里面
int [] hash_old = new int[257];
int [] hash_new = new int[257];
for(int i =0;i<p.length();i++){
hash_old[p.charAt(i)]++;
}
for(int i= 0 ;i<=s.length()-p.length();i++){
for(int k =0;k<p.length();k++) {
hash_new[p.charAt(k)] = hash_old[p.charAt(k)];
}
int j ;
for(j = 0; j<p.length();j++){
if(hash_new[s.charAt(i+j)]<=0) break;
if(hash_new[s.charAt(i+j)] > 0) hash_new[s.charAt(i+j)]--;
}
if(j==p.length()) list.add(i);
}
return list;
}
}