Skip to content

Commit da0dca2

Browse files
committed
Create 438-Find-All-Anagrams-In-A-String.kt
1 parent d49b16e commit da0dca2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: kotlin/438-Find-All-Anagrams-In-A-String.kt

+31
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)