438. Find All Anagrams in a Stringjava
題目大意:給兩個字符串,s和p,求p在s中出現的位置,p串中的字符無序,ab=bathis
思路:起初想的是求p的全排列,保存到set中,遍歷s,若是在set中出現,s中的第一個字符位置保存到結果中,最後返回結果。這種思路執行超時。多是求全排列超時的。.net
思路2:先把p中的字符及字符出現的次數統計出來保存到map中,再遍歷s,這個思路和169. Majority Element - LeetCode中提到的創新解法相似code
Java實現:blog
public List<Integer> findAnagrams(String s, String p) { List<Integer> ans = new ArrayList<>(); if (s.length() <= p.length()) return ans; // 構造map,並初始化target Map<Character, Bo> map = new HashMap<>(); for (char tmp : p.toCharArray()) { Bo bo = map.get(tmp); if (bo == null) { bo = new Bo(); map.put(tmp, bo); } bo.target++; } // 前p.length()項 for (int i = 0; i < p.length(); i++) { char cur = s.charAt(i); Bo bo = map.get(cur); if (bo != null) { bo.cur++; } } if (allOne(map)) ans.add(0); for (int i = p.length(); i < s.length(); i++) { char cur = s.charAt(i); if (map.get(cur) != null) map.get(cur).cur++; char last = s.charAt(i - p.length()); if (map.get(last) != null) map.get(last).cur--; if (allOne(map)) ans.add(i - p.length() + 1); } return ans; } public boolean allOne(Map<Character, Bo> map) { for (Map.Entry<Character, Bo> entry : map.entrySet()) { if (entry.getValue().cur != entry.getValue().target) return false; } return true; } class Bo { int cur; // 當前數 int target; // 目標數 public Bo() { this(0, 0); } public Bo(int cur, int target) { this.cur = cur; this.target = target; } }
欣賞一下別人寫的,所說下面兩道題用的是同一思路,記錄一下ip
public List<Integer> findAnagrams(String s, String p) { List<Integer> list = new ArrayList<>(); if (s == null || s.length() == 0 || p == null || p.length() == 0) return list; int[] hash = new int[256]; //character hash //record each character in p to hash for (char c : p.toCharArray()) { hash[c]++; } //two points, initialize count to p's length int left = 0, right = 0, count = p.length(); while (right < s.length()) { //move right everytime, if the character exists in p's hash, decrease the count //current hash value >= 1 means the character is existing in p if (hash[s.charAt(right++)]-- >= 1) count--; //when the count is down to 0, means we found the right anagram //then add window's left to result list if (count == 0) list.add(left); //if we find the window's size equals to p, then we have to move left (narrow the window) to find the new match window //++ to reset the hash because we kicked out the left //only increase the count if the character is in p //the count >= 0 indicate it was original in the hash, cuz it won't go below 0 if (right - left == p.length() && hash[s.charAt(left++)]++ >= 0) count++; } return list; }
public List<Integer> findAnagrams(String s, String p) { char[] ptrn = p.toCharArray(); char[] str = s.toCharArray(); int[] w = new int[26]; for(char c : ptrn) w[c - 'a']++; int start = 0; List<Integer> result = new LinkedList<>(); for(int i = 0; i<str.length; i++){ int cIndex = str[i] - 'a'; w[cIndex]--; // the crucial bit, if we have seen the character too many times // or it is a character that is not in the pattern, rewind the starting index while(w[cIndex] < 0){ w[str[start] - 'a']++; start++; } if(i - start + 1 == ptrn.length){ result.add(start); w[str[start] - 'a']++; start++; } } return result; }