We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d49b16e commit da0dca2Copy full SHA for da0dca2
kotlin/438-Find-All-Anagrams-In-A-String.kt
@@ -0,0 +1,31 @@
1
+class Solution {
2
+ fun findAnagrams(s: String, p: String): List<Int> {
3
+
4
+ val pCount = IntArray(26)
5
+ val res = ArrayList<Int>()
6
7
+ for(c in p)
8
+ pCount[c - 'a']++
9
10
+ var start = 0
11
+ var end = 0
12
13
+ while(end < s.length){
14
+ // increase the window
15
+ if(pCount[s[end] - 'a'] > 0){
16
+ pCount[s[end++] - 'a']--
17
+ if(end-start == p.length)
18
+ res.add(start)
19
+ // window size 0? step to next
20
+ }else if(start == end){
21
+ start++
22
+ end++
23
+ //decrease the window
24
+ }else{
25
+ pCount[s[start++] - 'a']++
26
+ }
27
28
29
+ return res
30
31
+}
0 commit comments