讨论/技术交流/438题,最后一例过不去....郁闷/
438题,最后一例过不去....郁闷

class Solution {
public List<Integer> findAnagrams(String s, String p) {
Map<Character,Integer> map = new HashMap<>();
Map<Character,Integer> need = new HashMap<>();
List<Integer> list = new ArrayList<>();
for (char k:p.toCharArray()){
need.put(k,need.getOrDefault(k,0)+1);
}
int left=0,right=0;
int valid = 0;
while(right < s.length()){
char c = s.charAt(right);
right ++;
if (need.containsKey(c)){
map.put(c,map.getOrDefault(c,0)+1);
if (map.get(c) == need.get(c)){
valid++;
}
}
while(right - left >= p.length()){
if (valid == need.size()) {
list.add(left);
}
char d = s.charAt(left);
left++;
if (need.containsKey(d)){
if (map.get(d) == need.get(d)){
valid--;
}
map.put(d,map.get(d)-1);
}
}
}
return list;
}
}

共 2 个回复

谢谢大佬

1

if (map.get(c) == need.get(c)){
=> if (map.get(c).equals(need.get(c))){

if (map.get(d) == need.get(d)){
=> if (map.get(d).equals(need.get(d))){
这两个地方改一下好像就可以了