0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Leetcode 438. Find All Anagrams in a String

0
Posted at

アプローチ

Brute force

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();
        int len = s.length() - p.length();
        int pLen = p.length();

        int[] cnt = new int[26];

        for (char pChar : p.toCharArray()) {
            cnt[pChar - 'a']++;
        }

        for (int i = 0; i <= len; i++) {
            int[] cntTemp = new int[26];
            for (int j = i; j < pLen + i; j++) {
                char sChar = s.charAt(j);
                cntTemp[sChar - 'a']++;
            }

            if (Arrays.equals(cnt, cntTemp)) {
                result.add(i);
            }
        }

        return result;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?