File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List<Integer> findAnagrams (String s, String p) {
3
+ int sn = s.length ();
4
+ int pn = p.length ();
5
+ List<Integer> res = new ArrayList<>();
6
+ if (sn<=0 || pn<=0 ) return res;
7
+
8
+ int [] pArr = new int [26 ];
9
+ for (int i = 0 ; i < pn; i ++)
10
+ {
11
+ char c = p.charAt (i);
12
+ pArr[(int )(c-' a' )] ++;
13
+ }
14
+
15
+ for (int i = 0 ; i <= sn-pn; i ++)
16
+ {
17
+ char c = s.charAt (i);
18
+ if (pArr[(int )(c-' a' )]==0 ) continue ;
19
+ if (isAnagram (s, i, i+pn-1 , pArr))
20
+ {
21
+ res.add (i);
22
+ }
23
+ }
24
+
25
+ return res;
26
+ }
27
+
28
+ private boolean isAnagram (String s, int start, int end, int [] pArr)
29
+ {
30
+ int [] sArr = new int [26 ];
31
+ for (int i = start; i <= end; i ++)
32
+ {
33
+ char c = s.charAt (i);
34
+ if (pArr[(int )(c-' a' )]==0 ) return false ;
35
+ sArr [(int )(c-' a' )] ++;
36
+ if (pArr[(int )(c-' a' )]<sArr [(int )(c-' a' )]) return false ;
37
+ }
38
+
39
+ return true ;
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments