Concise 97% Java Solution


  • 0
    public class Solution {
        public List<Integer> findAnagrams(String s, String p) {
            List<Integer> res = new ArrayList<>();
            int[] map = new int[256];
            for (char c : p.toCharArray()) {
                map[c]++;
            }
            int left = 0;
            int count = 0;
            for (int i = 0; i < s.length(); i++) {
                map[s.charAt(i)]--;
                while (map[s.charAt(i)] < 0) {
                    map[s.charAt(left++)]++;
                }
                if (i - left + 1 == p.length()) {
                    res.add(left);
                }
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.