Beat 99% Java solution, very easy


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

Log in to reply
 

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