File tree 5 files changed +145
-0
lines changed
algorithms/FindAllAnagramsInAString
5 files changed +145
-0
lines changed Original file line number Diff line number Diff line change @@ -183,6 +183,7 @@ All solutions will be accepted!
183
183
| 459| [ Repeated Substring Pattern] ( https://leetcode-cn.com/problems/repeated-substring-pattern/description/ ) | [ java/py/js] ( ./algorithms/RepeatedSubstringPattern ) | Easy|
184
184
| 581| [ Shortest Unsorted Continuous Subarray] ( https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/description/ ) | [ java/py/js] ( ./algorithms/ShortestUnsortedContinuousSubarray ) | Easy|
185
185
| 443| [ String Compression] ( https://leetcode-cn.com/problems/string-compression/description/ ) | [ java/py/js] ( ./algorithms/StringCompression ) | Easy|
186
+ | 438| [ Find All Anagrams In A String] ( https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/description/ ) | [ java/py/js] ( ./algorithms/FindAllAnagramsInAString ) | Easy|
186
187
187
188
# Database
188
189
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Find All Anagrams In A String
2
+ This problem is easy to solve by hashmap
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > findAnagrams (String s , String p ) {
3
+ List <Integer > res = new ArrayList <Integer >();
4
+ Map <Character , Integer > patternMap = new HashMap <Character , Integer >();
5
+ int patternLength = p .length (),
6
+ beg = 0 ,
7
+ length = 0 ,
8
+ i = 0 ;
9
+
10
+ for (char c : p .toCharArray ()) {
11
+ Integer count ;
12
+ if ((count = patternMap .get (c )) == null ) patternMap .put (c , 1 );
13
+ else patternMap .put (c , count + 1 );
14
+ }
15
+
16
+ Map <Character , Integer > copyPatternMap = new HashMap <Character , Integer >(patternMap );
17
+
18
+ while (i < s .length ()) {
19
+ Integer count ;
20
+ if ((count = copyPatternMap .get (s .charAt (i ))) == null ) {
21
+ copyPatternMap = new HashMap <Character , Integer >(patternMap );
22
+ length = 0 ;
23
+ beg = ++i ;
24
+ } else if (count > 0 ) {
25
+ copyPatternMap .put (s .charAt (i ), count - 1 );
26
+ length ++;
27
+ i ++;
28
+ } else {
29
+ while (s .charAt (beg ++) != s .charAt (i )) {
30
+ copyPatternMap .put (s .charAt (beg - 1 ), copyPatternMap .get (s .charAt (beg - 1 )) + 1 );
31
+ length --;
32
+ }
33
+ i ++;
34
+ }
35
+
36
+ if (length == patternLength ) {
37
+ res .add (beg );
38
+ copyPatternMap .put (s .charAt (beg ), copyPatternMap .get (s .charAt (beg ++)) + 1 );
39
+ length --;
40
+ }
41
+ }
42
+
43
+ return res ;
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @param {string } p
4
+ * @return {number[] }
5
+ */
6
+ var findAnagrams = function ( s , p ) {
7
+ let res = [ ] ,
8
+ patternLength = p . length ,
9
+ patternMap = { }
10
+ beg = 0 ,
11
+ length = 0 ,
12
+ i = 0
13
+
14
+ for ( let j = 0 ; j < patternLength ; j ++ ) {
15
+ if ( patternMap [ p [ j ] ] === undefined ) patternMap [ p [ j ] ] = 0
16
+ patternMap [ p [ j ] ] += 1
17
+ }
18
+
19
+ let copyPatternMap = JSON . parse ( JSON . stringify ( patternMap ) )
20
+
21
+ while ( i < s . length ) {
22
+ if ( length < patternLength ) {
23
+ if ( copyPatternMap [ s [ i ] ] === undefined ) {
24
+ copyPatternMap = JSON . parse ( JSON . stringify ( patternMap ) )
25
+ length = 0
26
+ i ++
27
+ beg = i
28
+ } else if ( copyPatternMap [ s [ i ] ] > 0 ) {
29
+ copyPatternMap [ s [ i ] ] --
30
+ length ++
31
+ i ++
32
+ } else {
33
+ while ( s [ beg ++ ] !== s [ i ] ) {
34
+ copyPatternMap [ s [ beg - 1 ] ] ++
35
+ length --
36
+ }
37
+ i ++
38
+ }
39
+ }
40
+
41
+ if ( length === patternLength ) {
42
+ res . push ( beg )
43
+ copyPatternMap [ s [ beg ] ] ++
44
+ length --
45
+ beg ++
46
+ }
47
+ }
48
+
49
+ return res
50
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def findAnagrams (self , s , p ):
3
+ """
4
+ :type s: str
5
+ :type p: str
6
+ :rtype: List[int]
7
+ """
8
+ res = []
9
+ pattern_length = len (p )
10
+ pattern_map = {}
11
+ for c in p :
12
+ if pattern_map .get (c ) == None :
13
+ pattern_map [c ] = 0
14
+ pattern_map [c ] += 1
15
+
16
+ copy_pattern_map = dict (pattern_map )
17
+ beg = 0
18
+ length = 0
19
+ i = 0
20
+
21
+ while i < len (s ):
22
+ if length < pattern_length :
23
+ if copy_pattern_map .get (s [i ]) == None :
24
+ copy_pattern_map = dict (pattern_map )
25
+ length = 0
26
+ i += 1
27
+ beg = i
28
+ elif copy_pattern_map [s [i ]] > 0 :
29
+ copy_pattern_map [s [i ]] -= 1
30
+ length += 1
31
+ i += 1
32
+ else :
33
+ while s [beg ] != s [i ]:
34
+ copy_pattern_map [s [beg ]] += 1
35
+ length -= 1
36
+ beg += 1
37
+ # in this place s[beg] == s[i], so we have to go forward 1 distance
38
+ beg += 1
39
+ i += 1
40
+
41
+ if length == pattern_length :
42
+ res .append (beg )
43
+ copy_pattern_map [s [beg ]] += 1
44
+ length -= 1
45
+ beg += 1
46
+
47
+ return res
You can’t perform that action at this time.
0 commit comments